hihocoder 1108 : Troublesome Power Supply

来源:互联网 发布:千万不要相信网络拍卖 编辑:程序博客网 时间:2024/06/01 16:55

点击打开链接

描述

Little Hi's super computer has a complex power system consisting of N power supply units(PSUs). Each PSU has two possible states: turned on or off. Recently Little Hi found the power system was not running stable sometimes. After careful examination, he locates M malfunctioning pairs of PSUs. For each pair ai and bi, there is a certain state si(on or off) that when ai and bi are both turned to state si, the super computer becomes unstable.

Little Hi wants to know if there is a way to keep his computer running stable.

输入

Input contains several test cases.

The first line contains an integer T(T <= 10), the number of test cases.

For each test case, the first line contains two integers N(N <= 10000) and M(M <= 200000).

The i-th line of the following M lines contains three integers ai(1 <= ai <= N), bi(1 <= bi <= N) and si(0 for turned on and 1 for turn off) indicating that if PSU ai and bi are both turned to state si, the computer will become unstable.

输出

For each test case output "Yes" or "No" without qoutes.

样例输入
15 61 2 01 2 11 3 01 3 12 3 02 3 1
样例输出
No
#include<stdio.h>#include<stdlib.h>#include<iostream>#include<math.h>#include<vector>#include<string>#include<sstream>#include<algorithm>#include<stack>#include<queue>#include<limits.h>#include<numeric>#include<cstring>#include<map>#include<set>#ifndef _AAA#define _AAA//#include "a.h"//#include "a.cpp"#endifusing namespace std;#define eps 1e-8#define LL long longconst LL mod=12357;const int MIN= -1e3;const int MAX= 14 ;const int MAX_N= 2e5 + 10;const int MAX_M = 2e4 + 10;const int MAX_K = 1e4+10;static long long N, M, K, T, H, W, R;struct node{int to;int val;int next;}edge[MAX_N];int head[MAX_N];int dfn[MAX_N], low[MAX_N], tp[MAX_N];int cnt = 1, dfs_index, tpnum;bool instack[MAX_N];stack<int> mystack;int top;void addedge(int u, int v) {edge[cnt].to = v;edge[cnt].next = head[u];head[u] = cnt++;}void InputData(int n, int m){memset(head, 0, sizeof(head));cnt = 1;int x, y, s, x0, x1, y0, y1;while (m--){cin >> x >> y >> s;x0 = x<<1;x1 = x0|1;y0 = y<<1;y1 = y0|1;if(!s){addedge(x0, y1);addedge(y0, x1);}else{addedge(x1, y0);addedge(y1, x0);}}return;}void tarjan(int u){//dfn 节点访问次序编号//low 能指向的最小的节点。dfn[u] = low[u] = ++dfs_index;instack[u] = true;mystack.push(u);for(int i = head[u]; i != 0; i = edge[i].next){int v = edge[i].to;if( !dfn[v]){tarjan(v);low[u] = min(low[u], low[v]);}else {if(instack[v]) {low[u] = min(low[u], dfn[v]);}}}if(low[u] == dfn[u]) {tpnum ++;do {u = mystack.top();mystack.pop();tp[u] = tpnum;instack[u] = false;}while(low[u] != dfn[u] );}return;}bool judge(int n){for(int i=0;i<n; ++i){if(tp[i<<1] == tp[i<<1|1]) return false;}return true;}int main() {int cases;cin >> cases;while (cases--){int n, m;cin >> n >> m;InputData(n, m);memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));memset(instack, false, sizeof(instack));while (!mystack.empty()){mystack.pop();}dfs_index =  tpnum = 0;cnt = 1;for( int i =1; i<(n<<1); ++i){if(!dfn[i]) tarjan(i);}if(judge(n)) puts("Yes");else puts("No");}system("pause");return 0;}


0 0