codeforce 763A

Codeforces 763A-Timofey and a tree


题意:一棵树中n个节点被染上了c[i]颜色,让你在一棵树中随便选一个节点作为根节点,然后把整棵树抬起来,问你是否存在一个节点,使得以这个点为根节点的所有子树中的节点的颜色相同

解题思路:若有这样的节点,边的两端端点颜色不一样的边(设为特殊边,这样的边总数为m)肯定是有和根节点连在一起的(如果没有和根节点相连的话,肯定会造成子树里面有两个颜色不一样的);

  
int main()  
{  
    while(~scanf("%d",&n))  
    {  
        k=-1;  
        memset(x,0,sizeof x);  
        for(int i=1;i<n;i++)  
            scanf("%d %d",&u[i],&v[i]);  
        for(int i=1;i<=n;i++)  
            scanf("%d",&c[i]);  
        int sum=0;  
        for(int i=1;i<n;i++)  
        {  
            if(c[u[i]]!=c[v[i]])  
            {  
                sum++;  
                x[u[i]]++,x[v[i]]++;  
            }  
        }  
        for(int i=1;i<=n;i++)  
        {  
            if(sum==x[i])  
            {  
                k=i;break;  
            }  
        }  
        if(k==-1) printf("NO\n");  
        else printf("YES\n%d\n",k);  
    }  
    return 0;  
}  


another thought   http://blog.youkuaiyun.com/black_miracle/article/details/54839354

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
    int to,nex,num;
}edge[200005];
int cnt,head[100005],num[100005],ans,vis[100005],lab,sol[100005];
void add(int u,int v){
    edge[cnt].to=v;
    edge[cnt].nex=head[u];
    head[u]=cnt++;
} 
void dfs(int t,int pre){
    int ss=num[t];
    for(int i=head[t];~i;i=edge[i].nex){
        int v=edge[i].to;
        if(vis[v])continue;
        vis[v]=1;
        if(pre==0)ss=num[v];//如果是根节点
        else{
            if(pre!=num[v]){//如果不相等,就说明变数字
                ans++;
                lab=t;//lab记录下一次搜的位置
                return ;
            }
        }
        dfs(v,ss);
    }
}
void solve(int t){
    if(sol[t]){//防止死循环
        printf("NO\n");
        return ;
    }
    sol[t]++;
    ans=0;
    memset(vis,0,sizeof(vis));
    vis[t]=1;
    dfs(t,0);
    if(ans>=2){
        printf("NO\n");
    }
    else if(ans==1){
        solve(lab);
    }
    else{
        printf("YES\n%d\n",t);
    }
}
int main(){
    int x,y,n,i,j;
    scanf("%d",&n);
    memset(head,-1,sizeof(head));
    for(i=1;i<n;i++){
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    for(i=1;i<=n;i++)scanf("%d",&num[i]);
    solve(1);
    return 0;
}


### Codeforces Problem or Contest 998 Information For the specific details on Codeforces problem or contest numbered 998, direct references were not provided within the available citations. However, based on similar structures observed in other contests such as those described where configurations often include constraints like `n` representing numbers of elements with defined ranges[^1], it can be inferred that contest or problem 998 would follow a comparable format. Typically, each Codeforces contest includes several problems labeled from A to F or beyond depending on the round size. Each problem comes with its own set of rules, input/output formats, and constraint descriptions. For instance, some problems specify conditions involving integer inputs for calculations or logical deductions, while others might involve more complex algorithms or data processing tasks[^3]. To find detailed information regarding contest or problem 998 specifically: - Visit the official Codeforces website. - Navigate through past contests until reaching contest 998. - Review individual problem statements under this contest for precise requirements and examples. Additionally, competitive programming platforms usually provide comprehensive documentation alongside community discussions which serve valuable resources when exploring particular challenges or learning algorithmic solutions[^2]. ```cpp // Example C++ code snippet demonstrating how contestants interact with input/output during competitions #include <iostream> using namespace std; int main() { int n; cin >> n; // Process according to problem statement specifics } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值