QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#638990#9459. Tree Equationucup-team3282AC ✓422ms46596kbC++204.5kb2024-10-13 17:32:142024-10-13 17:32:15

Judging History

你现在查看的是测评时间为 2024-10-13 17:32:15 的历史记录

  • [2024-10-13 18:43:07]
  • 管理员手动重测本题所有得分≥97分的提交记录
  • 测评结果:AC
  • 用时:428ms
  • 内存:47484kb
  • [2024-10-13 17:32:15]
  • 评测
  • 测评结果:100
  • 用时:422ms
  • 内存:46596kb
  • [2024-10-13 17:32:14]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<unordered_map>
#include<vector>
using namespace std;
long long mod=1e9+7;
long long prim[1000050];
long long pow(long long r,long long b=mod-2){
    if(b==1)return r;
    long long ret=pow(r,mod>>1);
    if(b&1)return ret*ret%mod*r%mod;
    else ret*ret%mod;
}
struct node{
    vector<int>lk;
    long long hash=1;
    int dep,sz;
}pt[500050];int tp=0;
void hsh(int p){
    if(pt[p].sz==0){
        pt[p].sz=1;pt[p].dep=1;
        for(auto x:pt[p].lk){
            hsh(x);
            pt[p].dep=max(pt[p].dep,pt[x].dep+1);
            pt[p].sz+=pt[x].sz;
            pt[p].hash=(pt[p].hash*pt[x].hash)%mod;
        }
        pt[p].hash=pt[p].hash* prim[ pt[p].sz ]%mod;
    }
    return;
}
int a[500050],b[500050],c[500050];
int fd(int rt,int dep){
    dep=pt[dep].dep;
    int p=rt;
    for(int i=1;i<dep;i++){
        if(pt[p].lk.size()==0)return -1;
        int r=pt[p].lk[0]; 
        for(auto x:pt[p].lk){
            if(pt[x].dep>pt[r].dep)r=x;
        }
        p=r;
    }
    return p;
}
long long multiply(int rt1,int rt2){
    long long ret=1;
    for(auto x:pt[rt2].lk){
        ret=ret*pt[x].hash%mod;
    }
    for(auto x:pt[rt1].lk){
        ret=ret*multiply(x,rt2)%mod;
    }
    ret=ret*prim[pt[rt1].sz*pt[rt2].sz]%mod;
    return ret;
}
long long newroot(int rt,int rt1,int rt2){
    int nrt=++tp;
    unordered_map<int,int>mp;
    for(auto x:pt[rt2].lk)mp[pt[x].hash]++;
    for(auto x:pt[rt1].lk)mp[multiply(x,rt2)]++;
    for(auto x:pt[rt].lk){
        if(mp[pt[x].hash]>0){
            mp[pt[x].hash]--;
        }else{
            pt[nrt].lk.push_back(x);
        }
    }
    for(auto x:mp){
        if(x.second!=0){
            return -1;
        }
    }
    hsh(nrt);
    return nrt;
}
int cnt=0;
void otp(int p,int fa){
    cout<<fa<<" ";
    int id=++cnt;
    for(auto x:pt[p].lk){
        otp(x,id);
    }
}
int main(){
    for(int i=0;i<=1000005;i++){
        prim[i]=(114514+i*19260817)%mod;
    }
    int t;cin>>t;while(t--){
        while(tp>0){
            pt[tp].lk.clear();
            pt[tp].hash=1;
            pt[tp].dep=pt[tp].sz=0;
            tp--;
        }
        int na,nb,nc;
        cin>>na>>nb>>nc;
        for(int i=1;i<=na;i++){
            a[i]=++tp;
            int f;cin>>f;
            if(f!=0){
                pt[a[f]].lk.push_back(a[i]);
            }
        }
        for(int i=1;i<=nb;i++){
            b[i]=++tp;
            int f;cin>>f;
            if(f!=0){
                pt[b[f]].lk.push_back(b[i]);
            }
        }
        for(int i=1;i<=nc;i++){
            c[i]=++tp;
            int f;cin>>f;
            if(f!=0){
                pt[c[f]].lk.push_back(c[i]);
            }
        }
        hsh(a[1]);
        hsh(b[1]);
        hsh(c[1]);
        //a->b
        // for(int i=1;i<=17;i++){
        //     cout<<i<<endl<<"->";
        //     for(auto x:pt[i].lk){
        //         cout<<x<<" ";
        //     }cout<<endl;
        // }
        // cout<<pt[a[1]].dep<<endl;
        bool flag=false;
        int fd1=fd(c[1],a[1]);
        if(fd1!=-1){
            // cout<<"A";
            // cout<<fd1<<"->";
            int nrt=newroot(c[1],a[1],fd1);
            int fd2=fd(nrt,b[1]);
            // cout<<fd2<<"->";
            if(fd2!=-1){
                if(pt[nrt].hash==multiply(b[1],fd2)){
                    //win!!!!!!!!!!!!!!!!!!!!!!!
                    cout<<pt[fd1].sz<<" "<<pt[fd2].sz<<endl;
                    cnt=0;otp(fd1,0);cout<<endl;
                    cnt=0;otp(fd2,0);cout<<endl;
                    flag=true;
                }
            }
        }
        if(flag)continue;
        //b->a
        fd1=fd(c[1],b[1]);
        if(fd1!=-1){
            // cout<<"B";
            // cout<<fd1<<"->";
            int nrt=newroot(c[1],b[1],fd1);
            int fd2=fd(nrt,a[1]);
            // cout<<fd2<<"->";
            if(fd2!=-1){
                if(pt[nrt].hash==multiply(a[1],fd2)){
                    //win!!!!!!!!!!!!!!!!!!!!!!!
                    cout<<pt[fd2].sz<<" "<<pt[fd1].sz<<endl;
                    cnt=0;otp(fd2,0);cout<<endl;
                    cnt=0;otp(fd1,0);cout<<endl;
                    flag=true;
                }
            }
        }
        if(flag)continue;
        cout<<"Impossible"<<endl;
    }
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

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

input:

2
2 3 10
0 1
0 1 2
0 1 1 3 4 3 6 3 1 9
4 3 10
0 1 2 2
0 1 2
0 1 1 3 4 3 6 3 1 9

output:

Impossible
2 1
0 1 
0 

result:

ok 2 cases passed

Test #2:

score: 0
Accepted
time: 422ms
memory: 46596kb

input:

11122
3 3 11
0 1 1
0 1 1
0 1 1 1 4 4 1 1 8 8 1
7 2 10
0 1 2 2 2 1 1
0 1
0 1 2 1 1 5 5 5 1 1
7 8 14
0 1 2 1 1 1 1
0 1 2 1 1 1 1 1
0 1 1 3 1 1 1 1 1 1 1 11 1 1
4 8 11
0 1 1 1
0 1 1 1 1 1 6 6
0 1 1 1 1 1 6 6 1 1 1
3 4 13
0 1 1
0 1 1 1
0 1 1 3 1 5 1 1 8 1 10 1 12
11 2 14
0 1 2 1 4 4 4 1 1 1 1
0 1
0 1 1 ...

output:

3 1
0 1 1 
0 
1 2
0 
0 1 
1 1
0 
0 
1 1
0 
0 
2 2
0 1 
0 1 
1 2
0 
0 1 
1 5
0 
0 1 2 1 1 
1 1
0 
0 
8 2
0 1 1 3 3 3 1 1 
0 1 
1 1
0 
0 
4 1
0 1 1 1 
0 
3 1
0 1 1 
0 
5 1
0 1 2 1 1 
0 
1 1
0 
0 
1 1
0 
0 
1 1
0 
0 
1 1
0 
0 
2 1
0 1 
0 
5 1
0 1 1 1 1 
0 
1 1
0 
0 
1 3
0 
0 1 1 
1 2
0 
0 1 
3 1
0 1 1 ...

result:

ok 11122 cases passed

Test #3:

score: 0
Accepted
time: 3ms
memory: 36200kb

input:

1
5 5 49
0 1 1 3 1
0 1 2 1 2
0 1 2 3 4 1 6 7 8 9 1 11 12 13 14 11 16 17 18 19 1 21 22 23 24 1 26 26 1 1 30 31 31 30 30 35 36 36 35 30 40 41 41 40 1 45 46 46 45

output:

5 5
0 1 2 3 4 
0 1 2 2 1 

result:

ok 1 cases passed

Extra Test:

score: 0
Extra Test Passed