一眼,什么神题。。。

然后发现对每个点无脑bfs一下是O(mn)的。。。额好吧Σ( ° △ °|||)

觉得clrs的写法很好呢,学习了!

 

 1 /**************************************************************
 2     Problem: 3417
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:7648 ms
 7     Memory:12916 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11  
12 using namespace std;
13 const int N = 10010;
14 const int K = 1000010;
15  
16 struct edge {
17   int next, to;
18   edge() {}
19   edge(int _n, int _t) : next(_n), to(_t) {}
20 } e[N];
21  
22 struct Edge {
23   int next, to, v;
24   Edge() {}
25   Edge(int _n, int _t, int _v) : next(_n), to(_t), v(_v) {}
26 } E[K];
27  
28 int first[N], First[N], tot, Tot;
29  
30 struct queue {
31   int x, y;
32   queue() {}
33   queue(int _x, int _y) : x(_x), y(_y) {}
34 } q[N];
35  
36 int n, m, Q, l, r;
37 int pos[N][2], dis[N][2];
38  
39 int read() {
40   int x = 0;
41   char ch = getchar();
42   while (ch < '0' || '9' < ch)
43     ch = getchar();
44   while ('0' <= ch && ch <= '9')
45     (x *= 10) += ch - '0', ch = getchar();
46   return x;
47 }
48  
49 inline void add_edges(int x, int y) {
50   e[++tot] = edge(first[x], y), first[x] = tot;
51   e[++tot] = edge(first[y], x), first[y] = tot;
52 }
53  
54 inline void Add_Edge(int x, int y, int z) {
55   E[++Tot] = Edge(First[x], y, z), First[x] = Tot;
56 }
57  
58 inline void update(int x, int y, int z, int i) {
59   if (pos[x][y] < i)
60     pos[x][y] = i, dis[x][y] = z, q[++r] = queue(x, y);
61 }
62  
63 int main() {
64   int i, j, x, y, z, X, Y;
65   n = read(), m = read(), Q = read();
66   for (i = 1; i <= m; ++i)
67     add_edges(read(), read());
68   for (i = 1; i <= Q; ++i) {
69     x = read(), y = read(), z = read();
70     Add_Edge(x, y, z);
71   }
72   for (i = 1; i <= n; ++i) if (First[i]) {
73       l = 1, r = 0, update(i, 0, 0, i);
74       while (l <= r) {
75     X = q[l].x, Y = q[l].y, ++l;
76     for (x = first[X]; x; x = e[x].next)
77       update(e[x].to, Y ^ 1, dis[X][Y] + 1, i);
78       }
79       for (x = First[i]; x; x = E[x].next)
80     if ((E[x].to != i || first[i]) && pos[E[x].to][E[x].v & 1] == i && dis[E[x].to][E[x].v & 1] <= E[x].v) E[x].to = 0;
81     }
82   for (i = 1; i <= Tot; ++i)
83     puts(E[i].to ? "NIE" : "TAK");
84   return 0;
85 }
View Code

 

posted on 2015-01-28 10:29  Xs酱~  阅读(221)  评论(0编辑  收藏  举报