QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#851306 | #9881. Diverge and Converge | Wuyanru | WA | 1ms | 4324kb | C++14 | 3.1kb | 2025-01-10 17:28:14 | 2025-01-10 17:28:14 |
Judging History
answer
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
using ll=long long;
using ld=long double;
using pli=pair<ll,int>;
using pi=pair<int,int>;
template<typename A>
using vc=vector<A>;
template<typename A,const int N>
using aya=array<A,N>;
inline int read()
{
int s=0,w=1;char ch;
while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
inline ll lread()
{
ll s=0,w=1;char ch;
while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
struct G
{
set<pi>g[2005];int dis[2005];
inline int get(int u,int v)
{
auto it=g[u].lower_bound(pi(v,0));
return it->second;
}
inline void add(int u,int v,int w)
{
g[u].insert(pi(v,w));
g[v].insert(pi(u,w));
}
inline void E(int u,int v,int id=0)
{
if(!id) id=get(u,v);
g[u].erase(pi(v,id));
g[v].erase(pi(u,id));
}
inline void bfs(int u)
{
memset(dis,-1,sizeof(dis));dis[u]=0;
queue<int>que;que.push(u);
while(!que.empty())
{
int u=que.front();que.pop();
for(auto j:g[u])
{
int p=j.first;if(dis[p]!=-1) continue;
dis[p]=dis[u]+1,que.push(p);
}
}
}
}G1,G2;
bool vis[2005];
int d[2005];
int n,C,cnt;
inline aya<vc<int>,2> run(int rest)
{
if(rest==1) return {vc<int>(),vc<int>()};
int u=0;for(int i=1;i<=n;i++) if(!vis[i]&&d[i]<d[u]) u=i;
if(d[u]==2)
{
int a,b,id1,id2;vis[u]=1;
tie(a,id1)=*G1.g[u].begin();G1.E(u,a,id1);d[a]--;
tie(b,id2)=*G2.g[u].begin();G2.E(u,b,id2);d[b]--;
// printf("u=%d : %d %d %d %d\n",u,a,id1,b,id2);
auto ans=run(rest-1);
ans[0].push_back(id1);
ans[1].push_back(id2);
return ans;
}
G *g1=&G1,*g2=&G2;bool f=0;
if(g1->g[u].size()==2) swap(g1,g2),f=1;
int a,b,c,id1,id2,id3,mem=++cnt;
tie(a,id1)=*(g1->g[u].begin());g1->E(u,a,id1);
tie(b,id2)=*(g2->g[u].begin());g2->E(u,b,id2);
tie(c,id3)=*(g2->g[u].begin());g2->E(u,c,id3);
g2->add(b,c,mem);d[a]--,vis[u]=1;
auto ans=run(rest-1);
if(f) swap(ans[0],ans[1]);
int t=-1;
for(unsigned i=0;;i++) if(ans[1][i]==mem){ t=i;break;}
ans[1][t]=id3;
ans[0].insert(ans[0].begin()+t,id1);
ans[1].insert(ans[1].begin()+t,id2);
if(f) swap(ans[0],ans[1]);
return ans;
}
int main()
{
n=read();d[0]=0x3f3f3f3;
for(int i=1;i<n;i++)
{
int u=read(),v=read();
G1.add(u,v,i),d[u]++,d[v]++;
}
for(int i=1;i<n;i++)
{
int u=read(),v=read();
G2.add(u,v,i),d[u]++,d[v]++;
}
cnt=2*n;auto ans=run(n);
for(int i:ans[0]) printf("%d ",i);;putchar('\n');
for(int i:ans[1]) printf("%d ",i);;putchar('\n');
return 0;
}
/*
5
1 3
5 3
2 4
5 4
4 1
5 2
3 2
5 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3992kb
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: 4324kb
input:
2 1 2 2 1
output:
1 1
result:
ok Correct, N = 2
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 4020kb
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 4 1 3 6
result:
wrong answer Wrong, N = 7, not forming a tree