标签:
/************************************************ Author :powatr Created Time :2015-8-6 19:51:08 File Name :b.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> #pragma comment (linker, "/STACK:102400000,102400000"); using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAX = 1e5 + 10; const int MAXN = 7e5 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int head[MAX]; int du[2][MAX]; int sum[MAX]; int vis[MAXN], ans[MAXN]; int n, m, E, u, v; struct edge{ int u, v; int next; }a[MAXN]; void inti() { E = 0; memset(head, -1, sizeof(head)); memset(vis, 0, sizeof(vis)); memset(sum , 0, sizeof(sum)); memset(du, 0, sizeof(du)); memset(ans, 0, sizeof(ans)); } void add(int u, int v) { a[E].u = u; a[E].v = v; a[E].next = head[u]; head[u] = E++; } void dfs(int u, int y) { for(int i = head[u]; ~i; i = a[i].next){ if(vis[i]) { head[u] = a[i].next; //访问过就删去 continue; } int v = a[i].v; if(v!=u && du[y][v] < du[y^1][v]) continue; vis[i] = vis[i^1] = 1; if(i%2) ans[i/2] = y^1;//表示从u到v else ans[i/2] = y; du[y][u]++; du[y^1][v]++; head[u] = a[i].next; dfs(v,y); return; } } int main() { int T; scanf("%d", &T); while(T--){ scanf("%d%d", &n, &m); inti(); for(int i = 1 ; i <= m; i++){ scanf("%d%d", &u, &v); add(u, v); add(v, u); sum[v]++; sum[u]++; } for(int i = 1; i <= n; i++){ while(du[0][i] + du[1][i] < sum[i]){ if(du[0][i] <= du[1][i]) dfs(i, 0); else dfs(i, 1); } } for(int i = 0 ; i < m; i++) printf("%d\n", ans[i]); } return 0; }
HDU5348——DFS——MZL's endless loop
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4709139.html