zoukankan      html  css  js  c++  java
  • ACM: Mr. Kitayuta's Colorful Graph-并查集-解题报

    Mr. Kitayuta's Colorful Graph
    Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th query, he gives you two integers — ui and vi. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly. Input The first line of the input contains space-separated two integers — n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers — ai, bi (1 ≤ ai < bi ≤ n) and ci (1 ≤ ci ≤ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i ≠ j, (ai, bi, ci) ≠ (aj, bj, cj). The next line contains a integer — q (1 ≤ q ≤ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers — ui and vi (1 ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi. Output For each query, print the answer in a separate line. Sample Input Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Output 2 1 0 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Output 1 1 1 1 2 Hint Let's consider the first sample.
    
     The figure above shows the first sample.
    Vertex 1 and vertex 2 are connected by color 1 and 2.
    Vertex 3 and vertex 4 are connected by color 3.
    Vertex 1 and vertex 4 are not connected by any single color.


    题目还是比较水,很简单的并查集,思路清晰就够了;
    AC代码:
    #include"iostream"
    #include"cstdio"
    #include"cstring"
    #include"cmath"
    #include"algorithm"
    using namespace std;
    const int MX=110;
    int CL[101];
    struct nde {
    	int	p[MX];	//记录每个颜色下的根 
    } cmap[MX];
    
    struct node {
    	int a,b,c; //连接的点 a b 和颜色c 
    } side[MX];
    
    bool cmps(node a,node b) {
    	return a.c<b.c;
    }
    
    struct nod {
    	int u,v;	//查询组 
    } Que[MX];
    
    int find(int x,int c) {
    	return cmap[c].p[x]==x?x:(cmap[c].p[x]=find(cmap[c].p[x],c)); //找到在颜色C下的根。 
    }
    
    void init() {				//清空并初始化整个颜色图。 
    	for(int i=1; i<=100; i++)
    		for(int j=1; j<=100; j++) {
    			cmap[i].p[j]=j;
    		}
    }
    
    int main() {
    	int n,m,q;
    	while(~scanf("%d%d",&n,&m)) {
    		init();
    		for(int i=1; i<=m; i++) {		//把整个输入先全部记录下 
    			scanf("%d%d%d",&side[i].a,&side[i].b,&side[i].c);	
    		}
    		sort(side+1,side+1+m,cmps);//按照颜色排序; 
    		int tot=1;		//tot 记录颜色的种类 
    		CL[tot]=side[1].c;//记录下出现过的颜色 
    		for(int i=1; i<=m; i++) {
    			if(side[i].c!=CL[tot]) {	//如果下一个颜色与上一个颜色不同记录进数组  CL 
    				CL[++tot]=side[i].c;
    			}
    			int rt1=find(side[i].a,side[i].c);  	//找到在颜色 C 下的根 
    			int rt2=find(side[i].b,side[i].c);
    			if(rt1!=rt2) {					
    				cmap[side[i].c].p[rt2]=rt1;	//在颜色 C下将两点连接,更新根 
    			}
    		}
    		scanf("%d",&q);
    		for(int i=1; i<=q; i++) {
    			scanf("%d%d",&Que[i].u,&Que[i].v);	//单组输入查询 
    			int ans=0;
    			for(int j=1; j<=tot; j++) {			//每个出现过的颜色下都查询一次 
    				int rt1=find(Que[i].u,CL[j]);	
    				int rt2=find(Que[i].v,CL[j]);
    				if(rt1==rt2)ans++;				//如果是一个联通块答案加一 
    			}
    			printf("%d
    ",ans);					//输出该组的答案 
    		}
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    几个计算广告学的课程
    Jie Bao 牛人cv
    如果英剧《黑镜子》拍第三季,你来写一集剧本策划,你想写一个什么样的故事呢? 知乎
    设计模式之迭代器与组合模式
    javascript 时钟clock
    项目中的需求蔓延
    一个产生随机random markers 例子
    设计模式之模板方法模式
    设计模式之反(向)模式
    转:用函数式编程技术编写优美的 JavaScript
  • 原文地址:https://www.cnblogs.com/HDMaxfun/p/5705945.html
Copyright © 2011-2022 走看看