QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#850611#9881. Diverge and ConvergeWuyanruRE 0ms3976kbC++143.1kb2025-01-10 10:36:022025-01-10 10:36:04

Judging History

你现在查看的是最新测评结果

  • [2025-01-10 10:36:04]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3976kb
  • [2025-01-10 10:36:02]
  • 提交

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(u);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(a,c,id3);d[b]--,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);
    }
}
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;
}
/*
4
3 4
3 2
2 1
2 4
4 1
3 1
*/

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3932kb

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: 0ms
memory: 3976kb

input:

2
1 2
2 1

output:

1
1

result:

ok Correct, N = 2

Test #3:

score: 0
Accepted
time: 0ms
memory: 3876kb

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
Runtime Error

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:


result: