首页 > 其他分享 > Calling Circles UVA - 247

Calling Circles UVA - 247

时间:2023-04-13 11:23:12浏览次数:32  
标签:Circles int s2 s1 tot 247 UVA include id


如果两个人相互打电话(直接或间接),则说他们在同一个电话圈里。例如, a打给b,b打给c, c打给d,d打给a,则这4个人在同一个圈里;如果e打给f但f不打给e,则不能推 出e和f在同一个电话圈里。 输入n(n≤25)个人的m次电话,找出所有电话圈。人名只包含字 母,不超过25个字符,且不重复   对于一个有向图,Floyd 找环,传递闭包 F[ i ][ j ] |= F[i][k] &&F[k][j] F[i] [j] =1 表示i,j 在一个圈里    

#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <cstring>
using namespace std;
 const int N=300;
 int a[N][N],vis[N],n,m;
 int tot;
 map<string,int> id;
 map<int,string> mp;
 int cas=0;
 
 void solve(){
 	int i,j,k,x,y;
 	string s1,s2;
 	tot=0;
 	for(i=1;i<=m;i++){
 		cin>>s1>>s2;
 		if(!id.count(s1)) id[s1]=++tot,mp[tot]=s1;
 		if(!id.count(s2)) id[s2]=++tot,mp[tot]=s2;
 		a[id[s1]][id[s2]]=1;
	 }
	for(k=1;k<=n;k++)
	 for(i=1;i<=n;i++)
	  for(j=1;j<=n;j++)
	   a[i][j]|=(a[i][k]&&a[k][j]);
	printf("Calling circles for data set %d:\n",++cas);
	
	queue<string> q;
 	for(i=1;i<=n;i++){
 		if(vis[i]) continue;
 		vis[i]=1;
 		q.push(mp[i]);
 		for(j=i+1;j<=n;j++){
 			if(a[i][j]&&a[j][i]){
 				vis[j]=1; q.push(mp[j]);
			 }
		 }
		
		while(q.size()>1) 
		cout<<q.front()<<", ",
		q.pop();
		cout<<q.front() <<endl; q.pop();
	 }
 }
 signed main(){
 	//freopen("in","r",stdin);
 	while(cin>>n>>m,n){
 		memset(vis,0,sizeof vis);
 		memset(a,0,sizeof a);
 		id.clear(); mp.clear();
 		solve();
	 }
 }
 
 

 

标签:Circles,int,s2,s1,tot,247,UVA,include,id
From: https://www.cnblogs.com/towboa/p/17312802.html

相关文章

  • abc247_f Cards 题解
    Cards题意有\(N\)张卡片,每张卡片上都写有两个数字,第\(i\)张卡片上的数字分别为\(P_i,Q_i\)。同时,\(P=(P_1,P_2,\dots,P_N)\)和\(Q=(Q_1,Q_2,\dots,Q_N)\)都是\((1,2,\dots,N)\)的全排列。我们需要在\(N\)张卡片中选出一些卡片,并且\(1,2,\dots,N......
  • UVa 10049 Self-describing Sequence (自描述序列&二分递推)
    10049-Self-describingSequenceTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=34&page=show_problem&problem=990SolomonGolomb's self­describingsequence  istheonlynon­decreas......
  • Shanghai 2006 / UVa 1382 Distant Galaxy (枚举&扫描&动态维护)
    1382-DistantGalaxyTimelimit:3.000seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4128YouareobservingadistantgalaxyusingatelescopeabovetheAstronomyTower,......
  • UVa 514 Rails (water ver.)
    514-RailsTimelimit:3.000seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=455ThereisafamousrailwaystationinPopPushCity.Countrythereisincrediblyhilly.Thest......
  • UVa 11044 Searching for Nessy (water ver.)
    11044-SearchingforNessyTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=1985TheLochNessMonsterisamysteriousandunidentifiedanimalsaidtoinhabi......
  • UVa 846 Steps (数学)
    846-StepsTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=787Onestepsthroughintegerpointsofthestraightline.Thelengthofastepmustbenonnegat......
  • UVa 253 Cube painting (模拟)
    253-CubepaintingTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=189Wehaveamachineforpaintingcubes.Itissuppliedwiththreedifferentcolors:bl......
  • UVa 11498 Division of Nlogonia (water ver.)
    11498-DivisionofNlogoniaTimelimit:1.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2493TheProblemAftercenturiesofhostilitiesandskirmishesbetweenthefour......
  • UVa 11723 Numbering Roads (water ver.)
    11723-NumberingRoadsTimelimit:1.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2823Inmycountry,streetsdon’thavenames,eachofthemarejustgivenanumber......
  • UVa 10719 Quotient Polynomial (数学)
    10719-QuotientPolynomialTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=1660Apolynomialofdegree n canbeexpressedasIf k isanyintegerthenwecan......