QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#717354#9230. Routing K-Codesgates_orzWA 129ms29676kbC++203.7kb2024-11-06 17:42:032024-11-06 17:42:04

Judging History

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

  • [2024-11-06 17:42:04]
  • 评测
  • 测评结果:WA
  • 用时:129ms
  • 内存:29676kb
  • [2024-11-06 17:42:03]
  • 提交

answer

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

using LL=long long;
const int N=2e5+10;
const int M=N*2;
#define int LL
int n,m;
const LL INF=4294967296;
int fa[N];
void init(int n) {
    for(int i=1;i<=n;i++)fa[i]=i;
}
int find(int x) {
    if(fa[x]==x)return x;
    fa[x]=find(fa[x]);
    return fa[x];
}
void unionn(int x,int y) {
    fa[find(y)]=find(x);
}
int h[N],e[M],ne[M],idx;
void add(int a,int b) {
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}
int len;
int root1,root2;
int root;
int dep[N],f[N];
int d[N];
void dfs(int u,int fat) {
    dep[u]=dep[fat]+1;
    fa[u]=fat;
    if(len<dep[u])len=dep[u],root=u;
    for(int i=h[u];i!=-1;i=ne[i]) {
        int v=e[i];
        if(v==fat)continue;
        dfs(v,u);
    }
}
LL val[N];
bool work(LL x) {
    set<LL>st;
    vector<int>vis(n+1,0);
    int u=root2;
    while(u!=root1) {
        //cerr<<"u="<<u<<"\n";
        val[u]=x;
        vis[u]=1;
        //if(x>=INF)return false;
        st.insert(x);
        if(x)x*=2;
        else x=1;
        u=fa[u];
    }
    /*cerr<<"val: ";
    for(int i=1;i<=n;i++) {
        cerr<<val[i]<<" ";
    }
    cerr<<"\n";*/

    int flag=1;
    auto dfs2=[&](auto &self,int a,int fat)->void {
        //cerr<<"a="<<a<<" fat="<<fat<<"\n";
        if(!flag)return;
        for(int i=h[a];i!=-1;i=ne[i]) {
            int b=e[i];
            if(b==fat||vis[b])continue;
            vis[b]=1;
            if(st.find(val[a]/2)==st.end()) {
                val[b]=val[a]/2;
                st.insert(val[a]/2);
            }
            else if(st.find(val[a]*2)!=st.end()) {
                if(st.find(val[a]*2+1)==st.end()) {
                    val[b]=val[a]*2+1;
                    /*if(val[b]>=INF) {
                        flag=0;
                        return;
                    }*/
                    st.insert(val[b]);
                }
                else {
                    flag=0;
                    return;
                }
            }
            else {
                val[b]=val[a]*2;
                /*if(val[b]>=INF) {
                    flag=0;
                    return;
                }*/
                st.insert(val[b]);
            }
            self(self,b,a);
        }
    };

    u=root2;
    while(u!=root1) {
        dfs2(dfs2,u,0);
        u=fa[u];
        if(!flag)return false;
    }
    return true;
}
void solve() {
    cin>>n>>m;
    for(int i=1;i<=n;i++)h[i]=-1;
    idx=0;
    init(n);
    int sign=1;
    int cnt=0;
    for(int i=1;i<=m;i++) {
        int u,v;
        cin>>u>>v;
        d[u]++,d[v]++;
        //if(d[u]>3||d[v]>3)sign=0;
        add(u,v),add(v,u);
        if(find(u)==find(v)) {
            sign=0;
        }
        else unionn(u,v),cnt++;
    }
    if(cnt!=n-1)sign=0;
    if(!sign) {
        cout<<"NIE"<<"\n";
        return;
    }
    dfs(1,0);
    root1=root;
    memset(f,0,sizeof(f));
    memset(dep,0,sizeof(dep));
    len=0;
    dfs(root,0);
    root2=root;
    //cerr<<"root: "<<root1<<" "<<root2<<"\n";
    //cerr<<"fa: "<<fa[root2]<<"\n";
    LL ans=0;
    auto get_ans=[&]() {
        //cerr<<"val: ";
        for(int i=1;i<=n;i++) {
            ans+=val[i];
            //cerr<<val[i]<<" ";
        }
        //cerr<<"\n";
    };
    if(work(0)) {
        get_ans();
        cout<<ans<<"\n";
    }
    else {
        if(work(1)) {
            get_ans();
            cout<<ans<<"\n";
        }
        else cout<<"NIE"<<"\n";
    }
}
/*
4 3
1 2
1 3
1 4

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

 */
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T=1;
    //cin>>T;
    while(T--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #2:

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

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

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: 129ms
memory: 29676kb

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:

7722019681250060266

result:

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