标签:strong dep when enter his output art freopen cep
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3184 Accepted Submission(s): 1035
1 //2017-08-27 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #include <vector> 7 #include <iomanip> 8 #include <cmath> 9 10 using namespace std; 11 12 const int N = 5010; 13 const int M = N*N; 14 const double EPS = 1e-6; 15 int head[N], rhead[N], tot, rtot; 16 struct Edge{ 17 int to, next; 18 }edge[M], redge[M]; 19 20 void init(){ 21 tot = 0; 22 rtot = 0; 23 memset(head, -1, sizeof(head)); 24 memset(rhead, -1, sizeof(rhead)); 25 } 26 27 void add_edge(int u, int v){ 28 edge[tot].to = v; 29 edge[tot].next = head[u]; 30 head[u] = tot++; 31 32 redge[rtot].to = u; 33 redge[rtot].next = rhead[v]; 34 rhead[v] = rtot++; 35 } 36 37 vector<int> vs;//后序遍历顺序的顶点列表 38 bool vis[N]; 39 int cmp[N];//所属强连通分量的拓扑序 40 41 //input: u 顶点 42 //output: vs 后序遍历顺序的顶点列表 43 void dfs(int u){ 44 vis[u] = true; 45 for(int i = head[u]; i != -1; i = edge[i].next){ 46 int v = edge[i].to; 47 if(!vis[v]) 48 dfs(v); 49 } 50 vs.push_back(u); 51 } 52 53 //input: u 顶点编号; k 拓扑序号 54 //output: cmp[] 强连通分量拓扑序 55 void rdfs(int u, int k){ 56 vis[u] = true; 57 cmp[u] = k; 58 for(int i = rhead[u]; i != -1; i = redge[i].next){ 59 int v = redge[i].to; 60 if(!vis[v]) 61 rdfs(v, k); 62 } 63 } 64 65 //Strongly Connected Component 强连通分量 66 //input: n 顶点个数 67 //output: k 强连通分量数; 68 int scc(int n){ 69 memset(vis, 0, sizeof(vis)); 70 vs.clear(); 71 for(int u = 0; u < n; u++) 72 if(!vis[u]) 73 dfs(u); 74 int k = 0; 75 memset(vis, 0, sizeof(vis)); 76 for(int i = vs.size()-1; i >= 0; i--) 77 if(!vis[vs[i]]) 78 rdfs(vs[i], k++); 79 return k; 80 } 81 82 int n, m; 83 int a[10010], b[10010], c[10010]; 84 85 bool check(int len){ 86 init(); 87 for(int i = 0; i < len; i++){ 88 if(c[i] == 0){ 89 add_edge(a[i]+n, b[i]); 90 add_edge(b[i]+n, a[i]); 91 }else if(c[i] == 1){ 92 add_edge(a[i], b[i]); 93 add_edge(a[i]+n, b[i]+n); 94 add_edge(b[i], a[i]); 95 add_edge(b[i]+n, a[i]+n); 96 }else if(c[i] == 2){ 97 add_edge(a[i], b[i]+n); 98 add_edge(b[i], a[i]+n); 99 } 100 } 101 scc(n<<1); 102 for(int i = 0; i < n; i++) 103 if(cmp[i] == cmp[i+n]) 104 return false; 105 return true; 106 } 107 108 int main() 109 { 110 std::ios::sync_with_stdio(false); 111 //freopen("inputD.txt", "r", stdin); 112 int T; 113 cin>>T; 114 while(T--){ 115 cin>>n>>m; 116 for(int i = 0; i < m; i++) 117 cin>>a[i]>>b[i]>>c[i]; 118 int l = 0, r = m, mid, ans; 119 while(l <= r){ 120 mid = (l+r)/2; 121 if(check(mid)){ 122 ans = mid; 123 l = mid+1; 124 }else 125 r = mid-1; 126 } 127 cout<<ans<<endl; 128 } 129 130 return 0; 131 }
标签:strong dep when enter his output art freopen cep
原文地址:http://www.cnblogs.com/Penn000/p/7440877.html