QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#822091 | #9881. Diverge and Converge | YMH_fourteen | WA | 12ms | 26724kb | C++14 | 2.6kb | 2024-12-19 21:21:31 | 2024-12-19 21:21:32 |
Judging History
answer
// Author: YE Minghan & XY_Eleven
#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]<=3){x=i;break;}
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
{
assert(deg[x]==3);
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),swap(xe,ye),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;
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;
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;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
input:
4 1 2 2 3 3 4 1 2 2 4 2 3
output:
3 2 1 2 3 1
result:
ok Correct, N = 4
Test #2:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
2 1 2 2 1
output:
1 1
result:
ok Correct, N = 2
Test #3:
score: 0
Accepted
time: 0ms
memory: 3652kb
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:
6 2 5 1 4 3 3 2 5 1 4 6
result:
ok Correct, N = 7
Test #4:
score: -100
Wrong Answer
time: 12ms
memory: 26724kb
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:
74 680 71 796 138 272 779 822 40 984 903 974 912 383 948 550 744 571 2 346 144 486 538 769 471 24 140 69 715 75 168 639 552 369 970 216 551 305 682 745 967 63 319 191 692 278 459 410 114 360 705 813 188 935 921 794 30 59 878 869 476 737 234 73 341 991 129 320 422 439 784 516 151 335 253 358 402 164 ...
result:
wrong answer Wrong, N = 1000, not forming a tree