QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#265506 | #6503. DFS Order 3 | obss | TL | 0ms | 3884kb | C++17 | 1.4kb | 2023-11-25 18:54:08 | 2023-11-25 18:54:09 |
Judging History
answer
#include <bits/stdc++.h>
#define tests
#define int long long
const int N = 1e3 + 10;
using namespace std;
using LL = long long;
int g[N][N];
int st[N],ed[N],p[N];
bool del[N];
int find(int x){
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
void solve() {
int n; cin>>n;
if(n==1) return ;
vector<pair<int,int>>ans;
auto merge=[&](int a,int b){
int fa=find(a);int fb=find(b);
if(fa!=fb){
p[fa]=fb;
ans.push_back({a,b});
}
};
for(int i=1;i<=n;i++) p[i]=i;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) cin>>g[i][j];
st[i]=1;ed[i]=n; del[i]=0;
merge(g[i][1],g[i][2]);
}
while(ans.size()<n-1){
vector<int>ye;
for(int i=1;i<=n;i++){
if(ed[i]>=st[i]&&!del[g[i][ed[i]]]){
del[g[i][ed[i]]]=1; ye.push_back(g[i][ed[i]]);
}
}
for(auto v:ye){
for(int i=st[v];i<=ed[v];i++){
if(!del[g[v][i]]){
st[v]=i+1; merge(v,g[v][i]); break;
}
}
}
for(int i=1;i<=n;i++){
while(del[g[i][ed[i]]]) ed[i]--;
}
}
for(auto [x,y]:ans) cout<<x<<' '<<y<<endl;
}
signed main() {
cin.tie(0)->ios::sync_with_stdio(0);
cout.tie(0);
int t = 1;
#ifdef tests
cin >> t;
#endif
for(int _ = 1; _ <= t; _ ++) solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3884kb
input:
4 2 1 2 2 1 3 1 2 3 2 1 3 3 2 1 4 1 2 3 4 2 1 3 4 3 2 4 1 4 2 1 3 5 1 2 4 3 5 2 4 1 3 5 3 5 1 2 4 4 2 1 3 5 5 3 1 2 4
output:
1 2 1 2 3 2 1 2 3 2 4 2 1 2 2 4 3 5 3 1
result:
ok correct answer! (4 test cases)
Test #2:
score: -100
Time Limit Exceeded
input:
20000 10 1 2 4 5 6 7 3 8 10 9 2 1 4 5 6 7 3 8 10 9 3 8 1 2 4 5 6 7 10 9 4 5 6 7 1 2 3 8 10 9 5 4 6 7 1 2 3 8 10 9 6 7 4 5 1 2 3 8 10 9 7 6 4 5 1 2 3 8 10 9 8 3 1 2 4 5 6 7 10 9 9 10 1 2 4 5 6 7 3 8 10 1 2 4 5 6 7 3 8 9 10 1 4 3 8 2 9 6 5 7 10 2 8 9 6 3 4 1 5 7 10 3 8 2 9 6 4 1 5 7 10 4 1 3 8 2 9 6 5...
output:
1 2 3 8 4 5 6 7 9 10 10 1 3 1 6 4 1 4