QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#697743 | #6503. DFS Order 3 | TJUHuangTao# | WA | 180ms | 3664kb | C++20 | 1.8kb | 2024-11-01 15:34:11 | 2024-11-01 15:34:12 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int, int>
#define ll long long
using namespace std;
map<pii,int> mp;
void solve(){
int n;
cin>>n;
mp.clear();
int a[n + 1][n + 1]{};
int adr[n + 1][n + 1]{};
int vis[n + 1][n + 1]{};
int noleaf[n + 1]{};
vector<int> edge[n + 1]{};
set<pii> q;
auto insert = [&](int u, int v)
{
q.insert({min(u, v), max(u, v)});
};
for (int i = 1;i<=n;i++){
for(int j=1;j<=n;j++)
cin >> a[i][j], adr[i][a[i][j]] = j;
for(int j=1;j<n;j++)
edge[a[i][j]].pb(a[i][j + 1]);
insert(a[i][1], a[i][2]);
}
if(n==2){
cout <<"1 2\n";
return;
}
set<pii> ans;
while(!q.empty()){
auto [u, v] = *q.begin();
q.erase(q.begin());
int flag1 = 0;
int flag2 = 0;
for (int i = 1; i <= n; i++)
{
if (i == u || i == v)
continue;
flag1 += adr[i][u] < adr[i][v];
flag2 += adr[i][u] > adr[i][v];
}
if(flag1)noleaf[u]=1;
if(flag2)noleaf[v]=1;
}
for (int i = 1;i<=n;i++){
vector<int>now;
now.pb(i);
for(int j=2;j<=n;j++)
{
now.pb(a[i][j]);
if(noleaf[a[i][j]]==0){
for (int k = 0; k < now.size() - 1;k++)
ans.insert({min(now[k], now[k + 1]), max(now[k], now[k + 1])});
now.clear();
}
}
}
for (auto [u, v] : ans)
cout << u << " " << v << '\n';
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;
cin>>t;
while(t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3536kb
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 2 3 1 2 2 3 2 4 1 2 1 3 2 4 3 5
result:
ok correct answer! (4 test cases)
Test #2:
score: -100
Wrong Answer
time: 180ms
memory: 3664kb
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 1 3 1 4 1 10 3 8 4 5 4 6 6 7 9 10 1 4 2 8 3 4 3 8 4 5 5 7 6 9 7 10 8 9 1 9 2 4 2 8 3 10 5 6 5 7 5 9 8 10 1 6 2 4 2 10 3 8 5 7 6 7 6 9 6 10 7 8 1 5 1 9 2 10 3 6 3 7 3 10 4 7 5 7 8 9 1 10 2 6 3 8 4 8 5 10 6 10 7 9 8 9 1 10 2 3 2 9 2 10 3 7 4 8 5 7 5 8 6 7 1 4 1 9 2 3 2 4 4 7 4 8 5 10 6 7 9 10 1 7 ...
result:
wrong answer your output is not a tree (test case 3)