题解 | #赢的次数#

赢的次数

https://ac.nowcoder.com/acm/contest/11223/A

D题暴力求解只求小于等于二的边即可 卡了我的输入输出我晕

using namespace std;
typedef long long ll;
const int maxn=1e6+5;

int sum1[maxn],sum2[maxn],son[maxn];
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
 inline void write(int x)
{
    char F[200];
     int tmp=x>0?x:-x ;
      if(x<0)putchar('-') ;
      int cnt=0 ;
         while(tmp>0)
         {
             F[cnt++]=tmp%10+'0';
            tmp/=10;
        }
        while(cnt>0)putchar(F[--cnt]) ;
 }
int main()
{
         ios::sync_with_stdio(0);
		 int n,k;
		 n=read();
		 for(int i=1;i<=n;i++)
		 son[i]=i;
         for(int i=2;i<=n;i++)
         {
         	int a,b;
         	a=read();
         	b=read();
         	if(b<=2)
         	{
         		if(b==1)
                {
         		   if(son[a]!=a)
         		   {
         		   	sum2[son[a]]++;
					}
         			sum1[i]++;
         			sum1[a]++;  
					 son[i]=a;
				 }
				 else
				 { 
         		    sum2[i]++;
         			sum2[a]++;
				 }
			 }
         	
		 }
       for(int i=1;i<=n;i++)
       {
       	     int h;
       	        if(i!=son[i])
			h=sum1[i]+sum2[i]+sum1[son[i]];
			 else
			  h=sum1[i]+sum2[i]+1;
			 
       	  write(h);
       	  printf("\n");
	   }
		   
	
		
    return 0;
}
全部评论
可以树上dfs做做 代码蛮简洁的
点赞 回复 分享
发布于 2022-03-29 10:27

相关推荐

05-03 12:45
西南大学 Java
nsnzkv:你这项目写的内容太多了,说实话都是在给自己挖坑,就算简历过了,后面面试也难受
点赞 评论 收藏
分享
05-20 13:59
门头沟学院 Java
米黑子米黑子:你这个成绩不争取下保研?
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务