标签:des style blog color io os java ar strong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 2319 Accepted Submission(s): 603
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <iostream> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 using namespace std; 11 #define N 10005 12 13 struct node{ 14 int x, y, id, id1, id2; 15 }ans[N]; 16 17 struct mem{ 18 int y, id, id1, id2; 19 }; 20 21 vector<mem>ve[N]; 22 map<string,int>ma; 23 map<int,string>mb; 24 int low[N], dfn[N]; 25 int visited[N]; 26 int n, m, dfn_clock; 27 int nu; 28 29 void init(){ 30 for(int i=0;i<=n;i++) ve[i].clear(); 31 ma.clear();mb.clear(); 32 memset(dfn,-1,sizeof(dfn)); 33 memset(visited,0,sizeof(visited)); 34 } 35 36 void dfs(int u,int fa){ 37 int i, j, k, v; 38 node p; 39 mem q; 40 //printf("1111111111\n"); 41 low[u]=dfn[u]=dfn_clock++; 42 43 visited[u]=1; 44 for(i=0;i<ve[u].size();i++){ 45 q=ve[u][i]; 46 if(q.y==fa) continue; 47 // printf("%d\n",v); 48 if(!visited[q.y]){ 49 dfs(q.y,u); 50 low[u]=min(low[u],low[q.y]); 51 if(low[q.y]>dfn[u]) { 52 p.x=u;p.y=q.y;p.id=q.id;p.id1=q.id1;p.id2=q.id2; 53 ans[nu++]=p; 54 } 55 } 56 else low[u]=min(low[u],dfn[q.y]); 57 58 } 59 } 60 61 bool cmp(node a,node b){ 62 return a.id<b.id; 63 } 64 65 main() 66 { 67 int t, i, j, k, id; 68 char s1[200], s2[200]; 69 mem p; 70 cin>>t; 71 while(t--){ 72 scanf("%d %d",&n,&m); 73 init();k=1;id=1; 74 while(m--){ 75 scanf("%s%s",s1,s2); 76 if(ma[s1]==0) ma[s1]=k,mb[k]=s1,++k; 77 if(ma[s2]==0) ma[s2]=k,mb[k]=s2,++k; 78 int x=ma[s1], y=ma[s2]; 79 p.y=ma[s2];p.id=id++;p.id1=1;p.id2=2; 80 ve[x].push_back(p); 81 p.y=ma[s1];p.id=id++;p.id1=2;p.id2=1; 82 ve[y].push_back(p); 83 } 84 85 dfn_clock=nu=0; 86 dfs(1,-1); 87 int f=1; 88 for(i=1;i<=n;i++) { 89 if(dfn[i]==-1){ 90 break; 91 } 92 } 93 if(i<=n){ 94 printf("0\n");continue; 95 } 96 sort(ans,ans+nu,cmp); 97 printf("%d\n",nu); 98 for(i=0;i<nu;i++){ 99 if(ans[i].id1<ans[i].id2) 100 cout<<mb[ans[i].x]<<" "<<mb[ans[i].y]<<endl; 101 else cout<<mb[ans[i].y]<<" "<<mb[ans[i].x]<<endl; 102 } 103 } 104 }
标签:des style blog color io os java ar strong
原文地址:http://www.cnblogs.com/qq1012662902/p/3968844.html