Source: Ural 1176. hyperchannels
Question: Find the Euler's loop of the supplementary Graph
Idea: Template
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxm = 40010;const int maxn = 1010;int first[maxn], cnt;struct edge{int u, v, next;}e[maxn*maxn];int ans[maxm];bool vis[maxm];int len;void AddEdge(int u, int v){e[cnt].u = u;e[cnt].v = v;e[cnt].next = first[u];first[u] = cnt++;}void dfs(int u){for(int i = first[u]; i != -1; i = e[i].next){if(!vis[i]){vis[i] = true;dfs(e[i].v);ans[len++] = i;}}}int main(){int n, s;while(scanf("%d %d", &n, &s) != EOF){memset(first, -1, sizeof(first));cnt = 0;for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){int x;scanf("%d", &x);if(!x && i != j){AddEdge(i, j);}}}memset(vis, 0, sizeof(vis));len = 0;dfs(s);for(int i = len-1; i >= 0; i--)printf("%d %d\n", e[ans[i]].u, e[ans[i]].v);}return 0;}
Ural 1176. hyperchannels Euler Loop