《std测试》

1:

#include<algorithm>
#include<stdio.h>
#include<iostream>
using namespace std;
const int maxn = 3e5+6;
int x[maxn],y[maxn];
int w[maxn];
int flag[maxn];
int fa[maxn];
int fi(int x)
{
    return x == fa[x]?x:fa[x]=fi(fa[x]);
}
int uni(int x,int y)
{
    int p = fi(x),q = fi(y);
    if(p != q)
        fa[p]=fa[q];
}
int main()
{
    int t;scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        int n,m;scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
            scanf("%d%d%d",&x[i],&y[i],&w[i]);
        int ans = 0;
        for(int i=30;i>=0;i--)
        {
            for(int j=1;j<=n;j++)
                fa[j]=j;
            for(int j=1;j<=m;j++)
            {
                if(((w[j]&ans)==ans)&&(w[j]>>i&1))
                    flag[j]=1;
                else
                    flag[j]=0;
            }
            for(int j=1;j<=m;j++)
                if(flag[j])
                    uni(x[j],y[j]);
            int p = fi(1);
            int f = 1;
            for(int j=1;j<=n;j++)
                if(fi(j)!=p)
                    f = 0;
            if(f)
                ans|=(1<<i);
        }
        cout<<ans<<endl;
    }
}
View Code

2:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,c;
} std1[300006];
int f[300006];
bool cmp(node n,node m)
{
    return n.c>m.c;
}
int find(int r)
{
    if(r==f[r])
    {
        return r;
    }
    else
    {
        f[r]=find(f[r]);
        return f[r];
    }
}
int merge(node n)
{
    int xx=find(n.x);
    int yy=find(n.y);
    if(xx!=yy)
    {
        f[yy]=xx;
        return 1;
    }
    return 0;
}
int main()
{
    int t,n,m,j,k,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        for(i=1; i<=n; i++)
        {
            f[i]=i;
        }
        for(i=1; i<=m; i++)
        {
            scanf("%d %d %d",&std1[i].x,&std1[i].y,&std1[i].c);
        }
        sort(std1+1,std1+m+1,cmp);
        int f=0;
        int cont=0;
        int sum;
        int k=1;
        for(i=1; i<=m; i++)
        {
            if(merge(std1[i])==1)
            {
                if(k==1)
                {
                    k=0;
                    merge(std1[i]);
                    sum=std1[i].c;
                }
                else if(k==0)
                {
                    sum&=std1[i].c;
                    merge(std1[i]);
                }
                cont++;
            }
            if(cont==n-1)
            {
                f=1;
            }
        }
        if(f==0)
        {
            printf("0\n");
        }
        else
        {
            printf("%d\n",sum);
        }
    }
    return 0;
}
View Code

3:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 311111
#define maxm 611111
 
struct node {
    int u, v;
    long long w;
};
vector <node> edge[2];
int n, m;
 
 
int fa[maxn];
int find (int x) {
    return fa[x] == x ? fa[x] : fa[x] = find (fa[x]);
}
bool ok (int id) { //判断能不能构成一个联通分量
    for (int i = 1; i <= n; i++)
        fa[i] = i;
    int Max = edge[id].size ();
    for (int i = 0; i < Max; i++) {
        int u = edge[id][i].u, v = edge[id][i].v;
        int p1 = find (u), p2 = find (v);
        if (p1 != p2)
            fa[p1] = p2 ;
    }
    int gg = find (1);
    for (int i = 2; i <= n; i++) {
        if (find (i) != gg)
            return 0;
    }
    return 1;
}
 
bool is_1 (long long num, int id) { //判断num的id位是不是1
    id--;
    num >>= id;
    return (num&1);
}
 
bool work (int id) { //判断id位能否为1
    int Max = edge[0].size ();
    for (int i = 0; i < Max; i++) {
        if (is_1 (edge[0][i].w, id)) {
            edge[1].push_back (edge[0][i]);
        }
    }
    if (ok (1)) {
        edge[0].clear ();
        int Max = edge[1].size ();
        for (int i = 0; i < Max; i++) {
            edge[0].push_back (edge[1][i]);
        }
        return 1;
    }
    return 0;
}
 
int main () {
    int t;
    scanf ("%d", &t);
    while (t--) {
        scanf ("%d%d", &n, &m);
        edge[0].clear (); edge[1].clear ();
        for (int i = 1; i <= m; i++) {
            int u, v; long long w;
            scanf ("%d%d%lld", &u, &v, &w);
            edge[0].push_back ((node){u, v, w});
        }
        if (!ok (0)) {
            printf ("0\n");
            continue;
        }
        long long ans = 0; 
        for (int i = 32; i >= 1; i--) { 
            edge[1].clear ();
            if (work (i)) { 
                ans <<= 1;
                ans++;
            }
            else 
                ans <<= 1;
        }
        printf ("%lld\n", ans);
    }
    return 0;
}
View Code

 

posted @ 2020-11-30 16:46  levill  阅读(136)  评论(0编辑  收藏  举报