标签:ios parent center lib mon pad queue strong color
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 575 Accepted Submission(s): 281
题目链接:HDU 6165
似乎比较模版的一道题目,一开始以为是一旦某个缩点后的点的出度超过2就不行了,实际上是可以的,比如$1 \to 2$,$2 \to 3$,$1 \to 3$,这样是三个连通分量且1的出度为2,但是任意取两个点$a,b$还是可以从$a$到达$b$或者$b$到达$a$的,因此更进一步应该是考虑拓扑序上是否同时存在两个可行的点,如果存在说明可以走分岔路这样一来至少分岔路上的两个点就是无法到达的
代码:
#include <stdio.h> #include <algorithm> #include <cstdlib> #include <cstring> #include <bitset> #include <string> #include <stack> #include <cmath> #include <queue> #include <set> #include <map> using namespace std; #define INF 0x3f3f3f3f #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) #define fin(name) freopen(name,"r",stdin) #define fout(name) freopen(name,"w",stdout) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); typedef pair<int, int> pii; typedef long long LL; const double PI = acos(-1.0); const int N = 10010; const int M = 60010; struct edge { int to, nxt; } E[M], e[M]; int head[N], tot; int dfn[N], low[N], st[N], ins[N], scc, in[N], belong[N], ts, top; int n, m; int H[N], Tot; void init() { CLR(head, -1); tot = 0; CLR(dfn, 0); CLR(low, 0); CLR(ins, 0); scc = 0; CLR(in, 0); ts = top = 0; CLR(H, -1); Tot = 0; } inline void add(int s, int t) { E[tot].to = t; E[tot].nxt = head[s]; head[s] = tot++; } inline void Add(int s, int t) { e[Tot].to = t; e[Tot].nxt = H[s]; H[s] = Tot++; } void Tarjan(int u) { dfn[u] = low[u] = ++ts; ins[u] = 1; st[top++] = u; int v; for (int i = head[u]; ~i; i = E[i].nxt) { v = E[i].to; if (!dfn[v]) { Tarjan(v); low[u] = min(low[u], low[v]); } else if (ins[v]) low[u] = min(low[u], dfn[v]); } if (low[u] == dfn[u]) { ++scc; do { v = st[--top]; ins[v] = 0; belong[v] = scc; } while (u != v); } } int solve() { queue<int>Q; int i, j; for (i = 1; i <= n; ++i) if (!dfn[i]) Tarjan(i); for (i = 1; i <= n; ++i) { for (j = head[i]; ~j; j = E[j].nxt) { int v = E[j].to; if (belong[v] == belong[i]) continue; ++in[belong[v]]; Add(belong[i], belong[v]); } } for (i = 1; i <= scc; ++i) if (!in[i]) Q.push(i); while (!Q.empty()) { if (Q.size() >= 2) return 0; int u = Q.front(); Q.pop(); for (int i = H[u]; ~i; i = e[i].nxt) { int v = e[i].to; if (--in[v] == 0) Q.push(v); } } return 1; } int main(void) { int T, a, b; scanf("%d", &T); while (T--) { init(); scanf("%d%d", &n, &m); while (m--) { scanf("%d%d", &a, &b); add(a, b); } solve() ? puts("I love you my love and our love save us!") : puts("Light my fire!"); } return 0; }
HDU 6165 FFF at Valentine(Tarjan缩点+拓扑排序)
标签:ios parent center lib mon pad queue strong color
原文地址:http://www.cnblogs.com/Blackops/p/7417569.html