HDU 3849 By Recognizing These Guys, We Find Social Networks Useful

By Recognizing These Guys, We Find Social Networks Useful

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on HDU. Original ID: 3849
64-bit integer IO format: %I64d      Java class name: Main
 
Social Network is popular these days.The Network helps us know about those guys who we are following intensely and makes us keep up our pace with the trend of modern times.
But how?
By what method can we know the infomation we wanna?In some websites,maybe Renren,based on social network,we mostly get the infomation by some relations with those "popular leaders".It seems that they know every lately news and are always online.They are alway publishing breaking news and by our relations with them we are informed of "almost everything".
(Aha,"almost everything",what an impulsive society!)
Now,it's time to know what our problem is.We want to know which are the key relations make us related with other ones in the social network.
Well,what is the so-called key relation?
It means if the relation is cancelled or does not exist anymore,we will permanently lose the relations with some guys in the social network.Apparently,we don't wanna lose relations with those guys.We must know which are these key relations so that we can maintain these relations better.
We will give you a relation description map and you should find the key relations in it.
We all know that the relation bewteen two guys is mutual,because this relation description map doesn't describe the relations in twitter or google+.For example,in the situation of this problem,if I know you,you know me,too.
 

Input

The input is a relation description map.
In the first line,an integer t,represents the number of cases(t <= 5).
In the second line,an integer n,represents the number of guys(1 <= n <= 10000) and an integer m,represents the number of relations between those guys(0 <= m <= 100000).
From the second to the (m + 1)the line,in each line,there are two strings A and B(1 <= length[a],length[b] <= 15,assuming that only lowercase letters exist).
We guanrantee that in the relation description map,no one has relations with himself(herself),and there won't be identical relations(namely,if "aaa bbb" has already exists in one line,in the following lines,there won't be any more "aaa bbb" or "bbb aaa").
We won't guarantee that all these guys have relations with each other(no matter directly or indirectly),so of course,maybe there are no key relations in the relation description map.
 

Output

In the first line,output an integer n,represents the number of key relations in the relation description map.
From the second line to the (n + 1)th line,output these key relations according to the order and format of the input.
 

Sample Input

1
4 4
saerdna aswmtjdsj
aswmtjdsj mabodx
mabodx biribiri
aswmtjdsj biribiri

Sample Output

1
saerdna aswmtjdsj

Source

 
解题:求割边
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 10010;
 4 struct arc {
 5     int to,next;
 6     bool cut;
 7     arc(int x = 0,bool y = false,int z = -1) {
 8         to = x;
 9         cut = y;
10         next = z;
11     }
12 } e[500000];
13 unordered_map<string,int>ump;
14 int head[maxn],dfn[maxn],low[maxn],clk,tot;
15 void add(int u,int v) {
16     e[tot] = arc(v,false,head[u]);
17     head[u] = tot++;
18 }
19 int ret;
20 void tarjan(int u,int fa) {
21     dfn[u] = low[u] = ++clk;
22     bool flag = false;
23     for(int i = head[u]; ~i; i = e[i].next) {
24         if(!flag && e[i].to == fa) {
25             flag = true;
26             continue;
27         }
28         if(!dfn[e[i].to]) {
29             tarjan(e[i].to,u);
30             low[u] = min(low[u],low[e[i].to]);
31             if(low[e[i].to] > dfn[u]) {
32                 e[i].cut = e[i^1].cut = true;
33                 ++ret;
34             }
35         } else low[u] = min(low[u],dfn[e[i].to]);
36     }
37 }
38 char name[200010][20];
39 void init() {
40     ump.clear();
41     for(int i = tot = clk = ret = 0; i < maxn; ++i) {
42         head[i] = -1;
43         dfn[i] = 0;
44     }
45 }
46 int main() {
47     int kase,n,m,p;
48     scanf("%d",&kase);
49     while(kase--) {
50         init();
51         scanf("%d%d",&n,&m);
52         for(int i = p = 0; i < m; ++i) {
53             scanf("%s",name[p+1]);
54             int a = ump[name[p+1]];
55             if(!a) {
56                 ump[name[p+1]] = a = p+1;
57                 ++p;
58             }
59             scanf("%s",name[p+1]);
60             int b = ump[name[p+1]];
61             if(!b) {
62                 ump[name[p+1]] = b = p + 1;
63                 ++p;
64             }
65             add(a,b);
66             add(b,a);
67         }
68         int cnt = 0;
69         for(int i = 1; i <= n; ++i)
70             if(!dfn[i]) {tarjan(i,-1);cnt++;}
71         if(cnt > 1){
72             puts("0");
73             continue;
74         }
75         printf("%d
",ret);
76         for(int i = 0; i < tot; i += 2)
77             if(e[i].cut) printf("%s %s
",name[e[i+1].to],name[e[i].to]);
78     }
79     return 0;
80 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4753078.html