QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#717319#9230. Routing K-Codesgates_orzWA 43ms19632kbC++203.6kb2024-11-06 17:35:442024-11-06 17:35:48

Judging History

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

  • [2024-11-06 17:35:48]
  • 评测
  • 测评结果:WA
  • 用时:43ms
  • 内存:19632kb
  • [2024-11-06 17:35:44]
  • 提交

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";*/
    u=root2;
    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()) {
                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);
        }
    };

    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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 15792kb

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 1ms
memory: 9748kb

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: 12008kb

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: 0
Accepted
time: 39ms
memory: 19328kb

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:

NIE

result:

ok single line: 'NIE'

Test #5:

score: -100
Wrong Answer
time: 43ms
memory: 19632kb

input:

200000 199999
168192 30733
164413 46673
175210 2329
69389 67102
33991 152553
91061 55265
166724 117726
90148 157176
34831 12213
60756 105488
121891 155192
82202 155666
102083 156661
38968 75200
190004 107321
72436 92732
64314 65004
39210 91106
70455 173568
179742 29844
126232 19552
133509 110602
131...

output:

NIE

result:

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