QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#107861 | #6503. DFS Order 3 | black_ice | WA | 0ms | 5432kb | C++14 | 1.5kb | 2023-05-23 00:05:15 | 2023-05-23 00:05:19 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <array>
#include <queue>
#include <cmath>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int,int> PII;
const int N = 1e3 + 10,mod = 998244353,INF = 1e18;
int g[N][N],n,T;
bool leaf[N],has[N][N];
int p[N];
vector<int> e[N];
int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
void solve()
{
cin >> n;
for(int i = 1;i <= n;i++) leaf[i] = false,p[i] = i,e[i].clear();
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
has[i][j] = false;
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
cin >> g[i][j];
if(j == n) leaf[g[i][j]] = true;
}
}
if(n == 1)
{
return ;
}
for(int i = 1;i <= n;i++)
{
int x = g[i][1],y = g[i][2];
if(x > y) swap(x,y);
if(!has[x][y])
{
e[x].push_back(y);
has[x][y] = true;
}
if(p[find(x)] != p[find(y)])
{
if(!leaf[find(y)]) p[find(x)] = find(y);
else p[find(y)] = find(x);
}
}
int cur = p[find(1)];
for(int i = 1;i <= n;i++)
{
if(p[find(i)] != cur)
{
e[cur].push_back(p[find(i)]);
p[find(i)] = cur;
}
}
for(int i = 1;i <= n;i++)
{
for(auto item : e[i])
{
cout << i << ' ' << item << endl;
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> T;
while(T--)
{
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 5432kb
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 2 4 2 3 3 5
result:
wrong answer ord[3] didn't pass the test (test case 4)