牛客练习赛24

传送

A

第一个位置放n种  因为相邻不可以放相同颜色,所以以后的每个位置放n-1种

#include 
using namespace std;
const int mod = 1e9 + 7;
int main()
{
    int n, m;
    scanf("%d%d",&n,&m);

    long long s = m;

    for(int i = 1;i < n;i ++)
    {
        s %= mod;
        s *= (m - 1);
    }
    s %= mod;
    cout << s << '\n';
    return 0;
}

 

B

并查集,去掉第一个点之后找最大的集合

#include 
using namespace std;
const int N = 1000005;

int fa[N],a[N];

int Find(int x)
{
    if(fa[x] == -1)
        return x;
    else
        return fa[x] = Find(fa[x]);
}

void Merge(int x,int y)
{
    int fx = Find(x);
    int fy = Find(y);

    fa[fx] = fy;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    memset(fa,-1,sizeof(fa));
    int n;
    cin >> n;
    for(int i = 1;i < n;i ++)
    {
        int x, y;
        cin >> x >> y;
        //if(x != 1 && y != 1)
        Merge(x, y);
    }

    for(int i = 1;i <= n;i ++)
        a[Find(i)] ++;

    int ans = 0;
    for(int i = 1;i <= n;i ++)
        ans = max(ans, a[i]);

    cout << ans <

 

C

模拟标记

#include 
using namespace std;
const int N = 1000005;
int n, m;
string s;
int red[N], blue[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    memset(red,-1,sizeof(red));
    memset(blue,-1,sizeof(blue));

    cin >> n >> m;
    cin >> s;
    int nr = 0,nb = 0;
    for(int i = 0;i < s.size();i ++)
    {
        if(s[i] == 'R')
            red[++nr] = i + 1;
        else
            blue[++nb] = i + 1;
    }

    string o;
    int x;
    for(int i = 1;i <= m;i ++)
    {
        cin >> o >> x;
        if(o[0] == 'R')
            cout << red[x] << '\n';
        else
            cout << blue[x] << '\n';
    }
    return 0;
}

 

D

普通的数加边递增处理  

注意坑点:  a,b同时为1的时候c可能有值,所有b为1的时候不能拿出来单独处理  (WA4次 逃)

#include 
using namespace std;
const int N = 5e4 + 5;
int v[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    int n;
    int a, b, c;
    cin >> n;
    for(int i = 1;i <= n;i ++)
    {
        cin >> a >> b >> c;
        v[a] = c + v[b];
    }
 
    int ans = -1;
    for(int i = 1;i < n;i ++)
        ans = max(ans, v[i]);
 
    cout << ans << endl;
    return 0;
}

 

E

迪杰斯特拉

#include 
using namespace std;
const int MAXN=1010;
const int INF=0x3f3f3f3f;
bool vis[MAXN];
int cost[MAXN][MAXN];
int lowcost[MAXN];
int n;
int m;
int u,v,c;
int s,e;
void Dijkstra(int s)
{
    for(int i=0;i<=n;i++)
    {
        lowcost[i]=INF;
        vis[i]=false;
    }
    lowcost[s]=0;
    for(int i=0;i<=n;i++)
    {
        int k=-1;
        int Min=INF;
        for(int j=0;j<=n;j++)
        {
            if(!vis[j] && lowcost[j]

更:

flody

#include
using namespace std;
typedef long long ll;
const int mod=1e9+7;
#define inf 0x3f3f3f3f
int mp[333][333];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    memset(mp,inf,sizeof mp);
    for(int i=0;i

F

背包,可以看作无限取

注意取模就好了,板子

#include 
using namespace std;
const int N = 1e5 + 5;
const int mod = 19260817;

int f[N], w[N];
int n, m;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m;
    for(int i = 1;i <= n;i ++)
        cin >> w[i];

    f[0] = 1;
    for(int i = 1;i <= n;i ++)
    {
        for(int j = w[i];j <= m;j ++)
        {
            f[j] += f[j - w[i]];
            f[j] %= mod;
        }
    }
    int s = 0;
    for(int i = 1;i <= m;i ++)
    {
        f[i] %= mod;
        s += f[i];
        s %= mod;

    }
    cout << s % mod << endl;

    return 0;
}

 

你可能感兴趣的:(牛客网,ACM)