Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool adj[9][9];
- bool visit[9];
- int lca[9][9];
- int find(int x)
- {
- return x == p[x] ? x : (p[x] = find(p[x]));
- }
- int DFS(int x)
- {
- if (visit[x]) return;
- visit[x] = true;
- for (int y=0; y<9; ++y)
- if (visit[y])
- lca[x][y] = lca[y][x] = find(y);
- // DFS
- for (int y=0; y<9; ++y)
- if (adj[x][y])
- {
- DFS(y);
- p[y] = x;
- }
- }
- void demo()
- {
- for (int i=0; i<9; ++i) p[i] = i;
- for (int i=0; i<9; ++i) visit[i] = false;
- DFS(0);
- int x, y;
- while (cin >> x >> y)
- cout << "" << lca[x][y];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement