QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#603774 | #3846. Link-Cut Tree | LJY_ljy | RE | 0ms | 0kb | C++20 | 1.9kb | 2024-10-01 19:01:21 | 2024-10-01 19:01:22 |
answer
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const long long MAXN = 1e5 + 10;
const long long MAXM = 1e5 + 10;
inline long long read() {
register long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
struct node {
long long to, index;
};
long long n, m, G[MAXM][2], fa[MAXN];
vector<node> G2[MAXN];
long long find(long long x) {
if (x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
long long unite(long long x, long long y) {
long long X = find(x);
long long Y = find(y);
if (X != Y) fa[X] = Y;
}
long long Start, End;
bool used[MAXM], Used[MAXM];
void dfs(long long u) {
if (u == End) {
for (long long i = 1; i <= m; i++)
Used[i] = used[i];
return;
}
for (long long i = 0; i < G2[u].size(); i++) {
if (!used[G2[u][i].index]) {
used[G2[u][i].index] = true;
dfs(G2[u][i].to);
used[G2[u][i].index] = false;
}
}
}
int main() {
long long t = read();
for (long long u = 1; u <= t; u++) {
n = read(); m = read();
for (long long i = 1; i <= m; i++) {
G[i][0] = read();
G[i][1] = read();
used[i] = false;
Used[i] = false;
}
for (long long i = 1; i <= n; i++) {
fa[i] = i;
G2[i].clear();
}
long long Maxx = -1;
for (long long i = 1; i <= m; i++) {
if (find(G[i][0]) == find(G[i][1])) {
Maxx = i;
break;
}
unite(G[i][0], G[i][1]);
G2[G[i][0]].push_back((node){G[i][1], i});
G2[G[i][1]].push_back((node){G[i][0], i});
}
if (Maxx == -1) printf("-1\n");
else {
Start = G[Maxx][0], End = G[Maxx][1];
dfs(Start);
for (long long i = 1; i < Maxx; i++) {
if (Used[i])
printf("%d ", i);
}
printf("%d\n", Maxx);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 6 8 1 2 2 3 5 6 3 4 2 5 5 4 5 1 4 2 4 2 1 2 4 3