QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#851057 | #9881. Diverge and Converge | Wuyanru | WA | 7ms | 4256kb | C++14 | 3.4kb | 2025-01-10 14:44:44 | 2025-01-10 14:44:45 |
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 p[2005];
int q[2005];
int d[2005];
int n,C;
inline void run(int u,G* G1,G* G2)
{
// printf("run %d\n",u);
if(G1->g[u].size()==2) swap(G1,G2);
int a=0,b=0,c=0,id1=0,id2=0,id3=0;
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->bfs(a);if(G2->dis[b]==-1) swap(b,c),swap(id2,id3);
// printf("(%d,%d) (%d,%d) (%d,%d)\n",a,id1,b,id2,c,id3);
// printf("p[%d]=%d q[%d]=%d\n",c,p[c],c,q[c]);
C++,p[C]=id1,q[C]=id2;G2->add(b,c,id3);d[a]--,vis[u]=1;
if(G1!=&::G1) swap(p[C],q[C]);
}
inline void run()
{
d[0]=0x3f3f3f3f;
for(int ID=1;ID<n;ID++)
{
int u=0;
for(int j=1;j<=n;j++) if(!vis[j]&&d[j]<d[u]) u=j;
assert(d[u]==2||d[u]==3);
if(d[u]==2)
{
// printf("2 u=%d\n",u);
auto it1=G1.g[u].begin(),it2=G2.g[u].begin();
C++,p[C]=it1->second,q[C]=it2->second;
vis[u]=1,d[it1->first]--,d[it2->first]--;
G1.E(u,it1->first,it1->second);
G2.E(u,it2->first,it2->second);
}
else run(u,&G1,&G2);
// for(int i=1;i<=n;i++) if(!vis[i])
// {
// printf("i=%d : d=%d\n",i,d[i]);
// for(auto j:G1.g[i]) printf("%d ",j.first);;putchar('\n');
// for(auto j:G2.g[i]) printf("%d ",j.first);;putchar('\n');
// }
}
}
int main()
{
n=read();
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]++;
}
run();
for(int i=1;i<=C;i++) printf("%d%c",p[i]," \n"[i==C]);
for(int i=1;i<=C;i++) printf("%d%c",q[i]," \n"[i==C]);
return 0;
}
/*
5
1 3
5 3
2 4
5 4
4 1
5 2
3 2
5 1
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 4256kb
input:
4 1 2 2 3 3 4 1 2 2 4 2 3
output:
1 3 2 1 2 3
result:
ok Correct, N = 4
Test #2:
score: 0
Accepted
time: 1ms
memory: 3952kb
input:
2 1 2 2 1
output:
1 1
result:
ok Correct, N = 2
Test #3:
score: 0
Accepted
time: 0ms
memory: 3972kb
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:
3 6 4 1 2 5 6 3 4 1 2 5
result:
ok Correct, N = 7
Test #4:
score: -100
Wrong Answer
time: 7ms
memory: 4232kb
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:
25 952 775 411 424 925 466 440 798 116 493 210 1 142 819 562 39 100 904 384 271 304 596 316 432 587 201 801 136 247 969 547 401 751 390 267 198 804 248 345 608 423 916 366 880 331 898 597 458 649 385 274 14 619 687 734 89 256 674 219 625 707 354 850 806 704 662 343 146 312 92 891 42 280 605 752 350 ...
result:
wrong answer Wrong, N = 1000, not forming a tree