QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#234144 | #3846. Link-Cut Tree | ucup-team1001# | RE | 0ms | 3604kb | C++23 | 1.4kb | 2023-11-01 14:29:19 | 2023-11-01 14:29:20 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define endl "\n"
#define yes cout<<"Yes"<<endl;
#define no cout<<"No"<<endl;
#define irep(i, l, r) for(int i = l; i <= r; ++ i)
void solve() {
int n, m;
cin >> n >> m;
vector<int>fa(n);
auto find = [&](auto self, int x) -> int{
if(fa[x] == x)return fa[x];
fa[x] = self(self, fa[x]);
return fa[x];
};
auto merge = [&](int u, int v) -> void{
fa[find(find, u)] = find(find, v);
};
vector<int>ans;
vector<vector<int>>t(n);
vector<int>dis;
vector<int>vis(n);
vector<array<int,2>>e;
auto dfs = [&](auto self,int ed, int x) -> void{
vis[x] = 1;
if(x == ed){
ans = dis;
}
for(int eg : t[x]){
auto [u, v] = e[eg];
int y = u + v - x;
if(vis[y])continue;
dis.push_back(eg);
self(self, ed, y);
dis.pop_back();
}
vis[x] = 0;
};
irep(i, 0, n - 1)fa[i] = i;
irep(i, 0, m - 1){
int u, v;
cin >> u >> v;
u -- ,v -- ;
if(find(find, u) == find(find, v)){
dfs(dfs, u, v);
sort(ans.begin(), ans.end());
for(int xx : ans){
cout << xx + 1 << ' ';
}
cout << i + 1 << endl;
return;
}
merge(u, v);
e.push_back({u, v});
t[u].emplace_back(i);
t[v].emplace_back(i);
// e[m] = {u, v};
}
puts("-1");
return;
}
int main() {
IOS
int t;
cin >> t;
while (t--)solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3604kb
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
output:
2 4 5 6 -1
result:
ok 2 lines
Test #2:
score: -100
Runtime Error
input:
1658 9 20 6 4 5 8 1 7 2 3 3 8 3 1 4 8 5 3 7 6 1 6 4 9 4 1 9 3 2 5 4 5 8 9 1 8 4 2 5 7 3 6 14 78 13 12 10 8 2 10 6 13 2 14 13 1 14 10 9 6 2 13 8 3 9 8 5 6 14 12 8 1 9 14 9 5 12 6 5 10 3 12 10 4 8 5 9 10 6 8 1 6 12 4 3 13 1 5 10 3 13 9 10 13 2 5 4 7 7 1 7 6 9 3 2 12 1 10 9 1 1 14 3 1 3 4 11 1 11 6 7 1...