QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#653767#6613. Bitwise Exclusive-OR Sequencenahida_qaqWA 2ms9816kbC++171.5kb2024-10-18 20:36:202024-10-18 20:36:29

Judging History

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

  • [2024-10-18 20:36:29]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:9816kb
  • [2024-10-18 20:36:20]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define io ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb push_back
using namespace std;
const int N=1e5+5,mod1=1e9+7,mod2=998244353;
typedef pair<int,int> pi;
int a[N][40];
int st[N][40];
int n,m;
vector<pi>g[100005];
int mp[N];
int dfs(int u,int x,int i)
{
    mp[u]=1;
    st[u][i]=1;
    a[u][i]=x;
    int ans=x;
    for(auto [v,w]:g[u])
    {
        int y=(w>>i)&1;
        if(st[v][i])
        {
            int z=(a[v][i])&1;
            if(x^y!=z)return -1;
        }
        else
        {
            a[v][i]=x^y;
            int xx=dfs(v,a[v][i],i);
            if(xx==-1)return -1;
            ans+=xx;
        }
    }
    return ans;
}
void solve()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y,w;
        cin>>x>>y>>w;
        g[x].pb({y,w});
        g[y].pb({x,w});
    }
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(mp[i])continue;
        for(int j=0;j<=30;j++)
        {
            int ans1=dfs(i,0,j);
            int ans2=dfs(i,1,j);
            if(ans1==-1&&ans2==-1)
            {
                cout<<-1;
                return ;
            }
            if(ans1==-1)ans+=(ans2<<j);
            else if(ans2==-1)ans+=(ans1<<j);
            else ans+=(min(ans1,ans2)<<j);
        }
    }
    cout<<ans;
}
signed main()
{
    io;
    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: 2ms
memory: 9816kb

input:

3 2
1 2 1
2 3 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

3 3
1 2 1
2 3 1
1 3 1

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 8296kb

input:

5 5
3 4 12
3 1 20
2 5 16
1 4 24
4 5 19

output:

82

result:

wrong answer 1st numbers differ - expected: '58', found: '82'