zoukankan      html  css  js  c++  java
  • BestCoder Round #68 (div.2) tree(hdu 5606)

    tree

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 863    Accepted Submission(s): 409


    Problem Description
    There is a tree(the tree is a connected graph which contains n points and n1 edges),the points are labeled from 1 to n,which edge has a weight from 0 to 1,for every point i[1,n],you should find the number of the points which are closest to it,the clostest points can contain i itself.
     
    
    
    Input
    the first line contains a number T,means T test cases.

    for each test case,the first line is a nubmer n,means the number of the points,next n-1 lines,each line contains three numbers u,v,w,which shows an edge and its weight.

    T50,n105,u,v[1,n],w[0,1]
     
    
    
    Output
    for each test case,you need to print the answer to each point.

    in consideration of the large output,imagine ansi is the answer to point i,you only need to output,ans1 xor ans2 xor ans3.. ansn.
     
    
    
    Sample Input
    1
    3
    1 2 0
    2 3 1
     
    
    
    Sample Output
    1
    in the sample.
    $ans_1=2$
    $ans_2=2$
    $ans_3=1$
    $2~xor~2~xor~1=1$,so you need to output 1.
     
    
    
    Source
     


    题意:有n个点和n-1条边,一条边连接两个点,每条边都有权值为0或1(只有这两个值)求出每个点的距离其最近的点的个数(两点之间距离为0包括自身在内)xi 然后输出所有xi异或的结果
    题解:先用并查集合并所有权值为0的边的两端点,(这样就分成多个块,每个块之间的任意两点之间的权值都为0)用一个数组s记录相同根节点的点的个数,遍历所有点则s[set[i]]就是距离i点最近的点的个数
    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<math.h>
    #include<algorithm>
    #define LL long long
    #define PI atan(1.0)*4
    #define DD doublea
    #define MAX 101000
    #define mod 10007
    using namespace std;
    int set[MAX];
    int s[MAX];
    int find(int fa)
    {
    	int ch=fa;
    	int t;
    	while(set[fa]!=fa)
    	    fa=set[fa];
    	while(ch!=fa)
    	{
    		t=set[ch];
    		set[ch]=fa;
    		ch=t;
    	}
    	return fa;
    }
    void mix(int x,int y)
    {
    	int fx=find(x);
    	int fy=find(y);
    	if(fx!=fy)
    	set[fx]=fy;
    }
    int main()
    {
        int n,m,j,i,t;
        int u,v,w; 
    	scanf("%d",&t);
    	while(t--)
    	{
    		scanf("%d",&n);
    		for(i=1;i<=n;i++)
    		{
    			set[i]=i;
    			s[i]=0;
    		}
    		
    		for(i=1;i<n;i++)
    		{
    			scanf("%d%d%d",&u,&v,&w);
    		    if(w==0)
    		        mix(u,v);
    		}
    		//printf("#
    ");
    		for(i=1;i<=n;i++)
    		{
    			int ans=find(i);
    			s[ans]++;
    		}
    		int ant=0;
    		for(i=1;i<=n;i++)
    		{
    			ant^=s[set[i]];
    		}
    		printf("%d
    ",ant);
    	}
    	return 0;
    } 
    

      

     
  • 相关阅读:
    Mac OS X系统下编译运行C代码
    Android intent传递list或对象
    Android中实现自定义的拍照应用
    由闭包引起的对javascript代码可维护性的思考
    让浏览器非阻塞加载javascript的几种方式
    Javascript中正则表达式的全局匹配模式
    解读JavaScript代码 var ie = !-[1,]
    Javascript中闭包的作用域链
    WPF代码注意事项,开发常见问题,知识总结
    WPF绑定各种数据源之object数据源
  • 原文地址:https://www.cnblogs.com/tonghao/p/5173447.html
Copyright © 2011-2022 走看看