QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#820410 | #9881. Diverge and Converge | YMH_fourteen | WA | 11ms | 26660kb | C++14 | 2.3kb | 2024-12-18 21:21:36 | 2024-12-18 21:21:36 |
Judging History
answer
// Author: YE Minghan
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "templates/debug.h"
#define __int128 ll
#else
#define dbg(...) (void)0
#define msg(...) (void)0
#endif
#define ll long long
#define endl '\n'
#define PB emplace_back
#define PPB pop_back
#define MP make_pair
#define ALL(Name) Name.begin(),Name.end()
#define PII pair<int,int>
#define VI vector<int>
#define GI greater<int>
#define fi first
#define se second
const int N=1005;
int n;
map<PII,int>id1,id2;
VI g[N];
bool dfs(int x,int fa,int tg)
{
if(x==tg)return 1;
for(int y:g[x])
if(y!=fa&&dfs(y,x,tg))return 1;
return 0;
}
pair<vector<PII>,vector<PII>> sol(vector<PII> xe,vector<PII> ye)
{
if(xe.empty())return {};
VI deg(n+1);
for(auto [u,v]:xe)deg[u]++,deg[v]++;
for(auto [u,v]:ye)deg[u]++,deg[v]++;
int x=0;deg[0]=4;
for(int i=1;i<=n;i++)
if(deg[i]&°[i]<deg[x])x=i;
if(deg[x]==2)
{
PII ex,ey;vector<PII> nx,ny;
for(auto [u,v]:xe)
if(u==x||v==x)ex={u,v};
else nx.PB(u,v);
for(auto [u,v]:ye)
if(u==x||v==x)ey={u,v};
else ny.PB(u,v);
tie(xe,ye)=sol(nx,ny);
xe.PB(ex),ye.PB(ey);return {xe,ye};
}
else
{
VI ex,ey;vector<PII> nx,ny;
for(auto [u,v]:xe)
if(u==x||v==x)ex.PB(u+v-x);
else nx.PB(u,v);
for(auto [u,v]:ye)
if(u==x||v==x)ey.PB(u+v-x);
else ny.PB(u,v);
bool swp=0;
if(ey.size()==2)swap(ex,ey),swap(nx,ny),swp=1;
int y=ex[0],z=ex[1],w=ey[0];
nx.PB(y,z);
tie(xe,ye)=sol(nx,ny);
auto it=find(ALL(xe),MP(y,z));
if(it==xe.end())it=find(ALL(xe),MP(z,y));
int pos=it-xe.begin();
int u=ye[pos].fi,v=ye[pos].se;
for(int i=1;i<=n;i++)g[i].clear();
for(auto [U,V]:ye)g[U].PB(V),g[V].PB(U);
if(dfs(u,v,w)^dfs(u,v,z))swap(y,z);
xe[pos]={x,y};
xe.emplace(xe.begin()+pos+1,x,z);
ye.emplace(ye.begin()+pos+1,x,w);
if(swp)swap(xe,ye);
return {xe,ye};
}
}
int main()
{
ios::sync_with_stdio(false),cin.tie(nullptr);
// int _;cin>>_;while(_--)
cin>>n;
vector<PII> x,y;
for(int i=1,u,v;i<n;i++)
{
cin>>u>>v;
if(u>v)swap(u,v);
x.PB(u,v),id1[MP(u,v)]=id1[MP(v,u)]=i;
}
for(int i=1,u,v;i<n;i++)
{
cin>>u>>v;
if(u>v)swap(u,v);
y.PB(u,v),id2[MP(u,v)]=id2[MP(v,u)]=i;
}
tie(x,y)=sol(x,y);
for(auto i:x)cout<<id1[i]<<" ";cout<<endl;
for(auto i:y)cout<<id2[i]<<" ";cout<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3644kb
input:
4 1 2 2 3 3 4 1 2 2 4 2 3
output:
2 3 1 3 2 1
result:
ok Correct, N = 4
Test #2:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
2 1 2 2 1
output:
1 1
result:
ok Correct, N = 2
Test #3:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
7 1 2 1 3 2 4 2 5 3 6 3 7 1 5 1 6 1 7 5 2 6 3 7 4
output:
5 2 1 4 6 3 5 2 1 4 3 6
result:
ok Correct, N = 7
Test #4:
score: -100
Wrong Answer
time: 11ms
memory: 26660kb
input:
1000 780 67 364 281 934 245 121 472 460 233 574 534 91 687 91 832 413 613 815 638 519 196 992 120 150 157 198 365 643 700 279 698 623 215 578 330 869 333 874 570 879 697 897 671 962 670 108 137 779 9 988 91 919 314 696 722 431 270 810 692 769 49 254 915 737 580 229 888 379 612 19 161 787 346 280 753...
output:
722 104 192 607 985 567 58 918 951 240 661 189 585 592 292 681 359 427 266 825 599 195 208 46 618 923 524 824 328 512 728 57 470 332 336 334 830 255 976 594 712 159 903 970 216 967 551 305 682 816 745 974 912 383 948 550 995 278 705 459 813 188 63 319 191 692 20 966 888 372 789 477 282 246 902 636 6...
result:
wrong answer Wrong, N = 1000, not forming a tree