• Codeforces_764_C. Timofey and a tree_(并查集)(dfs)


    C. Timofey and a tree
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the nits vertices, so that the i-th vertex gets color ci.

    Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can.

    Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex.

    A subtree of some vertex is a subgraph containing that vertex and all its descendants.

    Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed.

    Input

    The first line contains single integer n (2 ≤ n ≤ 105) — the number of vertices in the tree.

    Each of the next n - 1 lines contains two integers u and v (1 ≤ u, v ≤ nu ≠ v), denoting there is an edge between vertices u and v. It is guaranteed that the given graph is a tree.

    The next line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 105), denoting the colors of the vertices.

    Output

    Print "NO" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him.

    Otherwise print "YES" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them.

    Examples
    input
    4
    1 2
    2 3
    3 4
    1 2 1 1
    output
    YES
    2
    input
    3
    1 2
    2 3
    1 2 3
    output
    YES
    2
    input
    4
    1 2
    2 3
    3 4
    1 2 1 2
    output
    NO

    题意:给定一棵树,找一个点作为根节点,使得其所有子树,同一颗子树上的所有结点为同一个颜色。问是否能找到这样的结点。

    看几个同学和官方题解都是dfs。结果比赛时不知咋的,硬是用并查集做出来了。。。

    并查集思路:
    先建图,遍历每个结点,统计每个结点的度,对结点i,若其相邻的结点j与其颜色相同,那么将其合并,并将结点i和结点j的度都减1
    ,合并完后,所有颜色相同且相互邻接的点合并为一个集合,统计集合个数cnt,将一个集合看作一个点,在这个虚图中满足条件的点i
    一定满足cnt-1==deg[i],在合并后的原图中所有点中寻找满足cnt-1==deg[i]的点。

    代码:
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<vector>
    using namespace std;
    #define N 100005
    
    struct Node
    {
        int p,d;
    } deg[N];
    
    vector<int> gra[N];
    int col[N];
    
    
    
    bool cmp(Node a,Node b)
    {
        return a.d<b.d;
    }
    
    int father[N];
    int wei[N];
    int Find(int a)
    {
        if(father[a]!=a)
            father[a]=Find(father[a]);
        return father[a];
    }
    
    void Merge(int a,int b)
    {
        int fa=Find(a);
        int fb=Find(b);
        if(fa!=fb)
        {
            father[fa]=fb;
            wei[fb]+=wei[fa];
        }
    }
    
    int main()
    {
        int n;
        scanf("%d",&n);
        int a,b;
        for(int i=1; i<=n; i++)
        {
            deg[i].p=i;
            father[i]=i;
            wei[i]=1;
        }
        for(int i=0; i<n-1; i++)
        {
            scanf("%d%d",&a,&b);
            deg[a].d++;
            deg[b].d++;
            gra[a].push_back(b);
            gra[b].push_back(a);
        }
        for(int i=1; i<=n; i++)
            scanf("%d",&col[i]);
        for(int i=1; i<=n; i++)
            for(int j=0; j<gra[i].size(); j++)
            {
                if(col[i]==col[gra[i][j]])
                {
                    if(Find(i)!=Find(gra[i][j]))
                    {Merge(i,gra[i][j]);
                    deg[i].d--;
                    deg[gra[i][j]].d--;}
                }
            }
        int cnt=0;
        for(int i=1; i<=n; i++)
            if(father[i]==i)
                cnt++;
        /*printf("%d
    ",cnt);
        for(int i=1; i<=n; i++)
        {
            printf("%d",deg[i].d);
            if(i==n)
                printf("
    ");
            else
                printf(" ");
        }*/
        for(int i=1; i<=n; i++)
        {
            if(cnt==deg[i].d+1)
            {
                printf("YES
    %d
    ",i);
                return 0;
            }
        }
        printf("NO
    ");
        return 0;
    }

    dfs:感觉写的不好,用的vector,用邻接表感觉好些,一开始超时,然后开了个has数组,记忆化搜索,空间换时间。
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<cstring>
    using namespace std;
    #define N 100005
    
    vector<int> eage[N];
    int col[N];
    bool vis[N],vis1[N];
    int has[N];
    
    bool dfs(int node,int color)
    {
        if(col[node]!=color)
            return 0;
        //cout<<node<<'*'<<col[node]<<endl;
        vis[node]=1;
        if(has[node]==1)
            return 1;
        if(has[node]==-1)
            return 0;
        for(int i=0; i<eage[node].size(); i++)
        {
            if(vis[eage[node][i]]==0)
                if(dfs(eage[node][i],color)==0)
                {
                    has[node]=-1;
                    return 0;
                }
        }
        has[node]=1;
        return 1;
    }
    
    bool legal(int node)
    {
        memset(vis,0,sizeof(vis));
        vis[node]=1;
        for(int i=0; i<eage[node].size(); i++)
        {
            if(dfs(eage[node][i],col[eage[node][i]])==0)
            {
                //cout<<node<<":wrong"<<endl;
                return 0;
            }
        }
        //cout<<node<<":correct"<<endl;
        return 1;
    }
    
    int main()
    {
        int n;
        scanf("%d",&n);
        int a,b;
        for(int i=0; i<n-1; i++)
        {
            scanf("%d%d",&a,&b);
            eage[a].push_back(b);
            eage[b].push_back(a);
        }
        for(int i=1; i<=n; i++)
            scanf("%d",&col[i]);
        int allsame=1;
        for(int i=1; i<=n; i++)
            for(int j=0; j<eage[i].size(); j++)
            {
                if(col[i]!=col[eage[i][j]])
                {
                    allsame=0;
                    if(legal(i)&&vis1[i]==0)
                    {
                        printf("YES
    %d
    ",i);
                        return 0;
                    }
                   // has[i]=-1;
                    vis1[i]=1;
                    if(legal(eage[i][j])&&vis1[eage[i][j]]==0)
                    {
                        printf("YES
    %d
    ",eage[i][j]);
                        return 0;
                    }
                   // has[eage[i][j]]=-1;
                    vis1[eage[i][j]]=1;
                }
            }
        if(allsame==0)
            printf("NO
    ");
        else
            printf("YES
    1
    ");
        return 0;
    }
    
    
    
    邻接表:
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<cstring>
    using namespace std;
    #define N 100005
    
    struct Eage
    {
        int v,next;
    }eage[N<<1];
    int head[N],cnte;
    
    void addeage(int a,int b)
    {
        eage[cnte].v=b;
        eage[cnte].next=head[a];
        head[a]=cnte++;
    }
    
    int col[N];
    bool vis[N],vis1[N];
    int has[N];
    
    bool dfs(int node,int color)
    {
        if(col[node]!=color)
            return 0;
        //cout<<node<<'*'<<col[node]<<endl;
        vis[node]=1;
        if(has[node]==1)
            return 1;
        if(has[node]==-1)
            return 0;
        for(int i=head[node]; i>0; i=eage[i].next)
        {
            if(vis[eage[i].v]==0)
                if(dfs(eage[i].v,color)==0)
                {
                    has[node]=-1;
                    return 0;
                }
        }
        has[node]=1;
        return 1;
    }
    
    bool legal(int node)
    {
        memset(vis,0,sizeof(vis));
        vis[node]=1;
        for(int i=head[node]; i>0; i=eage[i].next)
        {
            if(dfs(eage[i].v,col[eage[i].v])==0)
            {
                //cout<<node<<":wrong"<<endl;
                return 0;
            }
        }
        //cout<<node<<":correct"<<endl;
        return 1;
    }
    
    int main()
    {
        int n;
        cnte=1;
        scanf("%d",&n);
        int a,b;
        for(int i=0; i<n-1; i++)
        {
            scanf("%d%d",&a,&b);
            addeage(a,b);
            addeage(b,a);
        }
        for(int i=1; i<=n; i++)
            scanf("%d",&col[i]);
        int allsame=1;
        for(int i=1; i<=n; i++)
            for(int j=head[i]; j>0; j=eage[j].next)
            {
                if(col[i]!=col[eage[j].v])
                {
                    allsame=0;
                    if(legal(i)&&vis1[i]==0)
                    {
                        printf("YES
    %d
    ",i);
                        return 0;
                    }
                   // has[i]=-1;
                    //vis1[i]=1;
                    if(legal(eage[j].v)&&vis1[eage[j].v]==0)
                    {
                        printf("YES
    %d
    ",eage[j].v);
                        return 0;
                    }
                   // has[eage[i][j]]=-1;
                    //vis1[eage[j].v]=1;
                }
            }
        if(allsame==0)
            printf("NO
    ");
        else
            printf("YES
    1
    ");
        return 0;
    }
    
    
    
     
  • 相关阅读:
    什么是.NET Core以及.NET Core能做什么 菜鸟飞不动
    SQL数据库连接字符串的几种写法整理
    高并发
    前端 防抖&节流,你学到未啊?
    Promise实现一个函数,通过fetch请求一个接口'/api/getdata'(可能成功,也可能失败),超过3秒钟请求未返回则认为超时
    手写实现deepClone方法
    手写Promise.retry方法;实现次数内重试请求
    element-ui的table表格通过子表数据,进行展示左侧展开箭头
    页面导出为PDF格式
    js自定义数字跳动效果computeNumber
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6401831.html
Copyright © 2020-2023  润新知