P1 : Troublesome Power Supply
-
1 5 6 1 2 0 1 2 1 1 3 0 1 3 1 2 3 0 2 3 1
Sample Output
-
No
Description
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
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.
Output
For each test case output "Yes" or "No" without qoutes.
思路:很裸的题目,a b 0 建边 !b -> a !a -> b,a b 1建边 b -> !a a -> !b。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (10000+10)
#define MAXM (500000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
struct Edge{
int from, to, next;
};
Edge edge[MAXM];
int head[MAXN*2], edgenum;
int dfn[MAXN*2], low[MAXN*2], sccno[MAXN*2], scc_cnt, dfs_clock;
void init(){
edgenum = 0;
CLR(head, -1);
}
void addEdge(int u, int v)
{
Edge E = {u, v, head[u]};
edge[edgenum] = E;
head[u] = edgenum++;
}
stack<int> S; bool Instack[MAXN*2];
void tarjan(int u, int fa)
{
dfn[u] = low[u] = ++dfs_clock;
S.push(u); Instack[u] = true;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(!dfn[v])
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(Instack[v])
low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u])
{
scc_cnt++;
for(;;)
{
int v = S.top(); S.pop();
Instack[v]= false;
sccno[v] = scc_cnt;
if(v == u)
break;
}
}
}
void find_cut(int l, int r)
{
CLR(dfn, 0); CLR(sccno, 0); CLR(low, 0); CLR(Instack, false);
scc_cnt = dfs_clock = 0;
for(int i = l; i <= r; i++)
if(!dfn[i]) tarjan(i, -1);
}
void getMap(int N, int M)
{
init();
W(M)
{
int a, b, op;
Ri(a); Ri(b); Ri(op);
if(op == 1)
{
addEdge(a, b+N);
addEdge(b, a+N);
}
else
{
addEdge(a+N, b);
addEdge(b+N, a);
}
}
}
void solve(int N)
{
bool flag = true;
for(int i = 1; i <= N; i++)
{
if(sccno[i] == sccno[i+N])
{
flag = false;
break;
}
}
printf(flag ? "Yes\n" : "No\n");
}
int main()
{
int t; Ri(t);
W(t)
{
int N, M;
Ri(N); Ri(M);
getMap(N, M);
find_cut(1, 2*N);
solve(N);
}
return 0;
}