QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#708972#9230. Routing K-CodesDrimpossibleWA 80ms27576kbC++142.3kb2024-11-04 10:23:152024-11-04 10:23:15

Judging History

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

  • [2024-11-04 10:23:15]
  • 评测
  • 测评结果:WA
  • 用时:80ms
  • 内存:27576kb
  • [2024-11-04 10:23:15]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define int unsigned long long
#define NO return cout<<"NIE",0

const int N=2e5+5;

int n,m,rt;
vector<int>e[N];

int dep[N],mx[2][N];
int f[N],g[N],mn[2][N];

void getdep(int u,int fa){
    for(int v:e[u]){
        if(v==fa)continue;
        dep[v]=dep[u]+1;
        getdep(v,u);
        if(mx[0][v]+1>mx[0][u])
            mx[1][u]=mx[0][u],mx[0][u]=mx[0][v]+1;
        else mx[1][u]=max(mx[1][u],mx[0][v]+1);
    }
}

void getmx(int u,int fa){
    for(int v:e[u]){
        if(v==fa)continue;
        if(mx[0][v]+1==mx[0][u])
            if(mx[1][u]+1>mx[0][v])
                mx[1][v]=mx[0][v],mx[0][v]=mx[1][u]+1;
        getmx(v,u);
    }
}

void dfs(int u,int fa){
    f[u]=1;
    mn[1][u]=1e18;
    for(int v:e[u]){
        if(v==fa)continue;
        dfs(v,u);
        f[u]+=(f[v]<<1);
        g[u]+=g[v];
        if(mn[0][u]==0)mn[0][u]=f[v];
        else if(f[v]<mn[0][u])
            mn[1][u]=mn[0][u],mn[0][u]=f[v];
        else mn[1][u]=min(mn[1][u],f[v]);
    }
    g[u]+=mn[0][u];
}

void fds(int u,int fa){
    for(int v:e[u]){
        if(v==fa)continue;
        f[v]+=(f[u]-f[v]*2)*2;
        int val=g[u]-g[v];
        if(mn[0][u]==f[v])
            val-=mn[0][u],val+=mn[1][u];
        g[v]+=val;
        
        val=(f[u]-f[v]*2)*2;
        if(mn[0][v]==0)mn[0][v]=val;
        else if(val<mn[0][v])
            mn[1][v]=mn[0][v],mn[0][v]=val;
        else mn[1][v]=min(mn[1][v],val);

        fds(v,u);
    }
}

signed main(){

    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);

    cin>>n>>m;
    if(m>n-1)NO;
    for(int i=1;i<=m;i++){
        int u,v;cin>>u>>v;
        e[u].emplace_back(v);
        e[v].emplace_back(u);
    }
    for(int i=1;i<=n;i++)
        if(e[i].size()>3)NO;
    getdep(1,0),getmx(1,0);
    mx[0][0]=1e18;
    for(int i=1;i<=n;i++)
        if(mx[0][i]<mx[0][rt]&&e[i].size()<3)rt=i;
    if(mx[0][rt]>33)NO;
    dfs(rt,0),fds(rt,0);

    int ans=1e18;
    for(int i=1;i<=n;i++){
        if(e[i].size()==3)continue;
        if(e[i].size()==1){
            if(mx[0][i]>31)continue;
            ans=min(ans,g[i]);
        }
        else {
            if(mx[0][i]>30)continue;
            ans=min(ans,f[i]+g[i]);
        }
    }
    if(ans==1e18)NO;
    cout<<ans;

    return 0;
}

詳細信息

Test #1:

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

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 2ms
memory: 10088kb

input:

4 6
1 2
2 3
3 4
4 1
1 3
2 4

output:

NIE

result:

ok single line: 'NIE'

Test #3:

score: 0
Accepted
time: 2ms
memory: 9080kb

input:

10 10
9 3
5 10
1 4
10 8
8 3
7 3
9 6
8 6
7 2
4 5

output:

NIE

result:

ok single line: 'NIE'

Test #4:

score: -100
Wrong Answer
time: 80ms
memory: 27576kb

input:

200000 199999
172774 188052
195063 155577
38023 148303
30605 155047
170238 19344
46835 58255
154268 109062
197059 116041
136424 12058
42062 149807
109545 115380
132322 51106
16706 162612
62234 31319
195735 80435
173898 84051
19876 102910
186924 9136
123094 117097
145054 173049
137364 152731
82662 18...

output:

4869343673573571

result:

wrong answer 1st lines differ - expected: 'NIE', found: '4869343673573571'