QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#725495#6613. Bitwise Exclusive-OR SequenceIsacBieber#RE 19ms80400kbC++232.0kb2024-11-08 18:18:472024-11-08 18:18:48

Judging History

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

  • [2024-11-08 18:18:48]
  • 评测
  • 测评结果:RE
  • 用时:19ms
  • 内存:80400kb
  • [2024-11-08 18:18:47]
  • 提交

answer

#include<bits/stdc++.h>
#define debug(x) cerr<<#x<<":"<<x<<'\n'
#define eb emplace_back
using namespace std;
using ll = long long;
using pii = pair <int,int>;
using pll = pair <ll,ll>;
const int N = 1e5 + 5, MOD = 1e9 + 7;
int n, m;
ll ans, a[N], c[N][32];
vector e(32, std::vector<std::vector<pii>>(N));
vector <int> vis;
bool dfs(int u,int bit,int fa)
{
    vis.push_back(u);
    for(auto son:e[u][bit])
    {
        int v = son.first, w = son.second;
        if(v!=fa)
        {
            if(c[v][bit]==-1)
            {
                if(w) c[v][bit] = (!c[u][bit]);
                else c[v][bit] = c[u][bit];
                if(!dfs(v,bit,u)) return false;
            }
            else if((c[v][bit]^c[u][bit])!=w) return false;
        }
    }
    return true;
}
void solve()
{
    cin>>n>>m;

    for(int i=1,u,v,w;i<=m;i++)
    {
        cin>>u>>v>>w;
        for(int j=0;j<=30;j++)
        {
            e[u][j].eb(v,(w>>j)&1);
            e[v][j].eb(u,(w>>j)&1);
        }
    }
    for(int bit=0;bit<=30;bit++)
    {
        for(int i=1;i<=n;i++) c[i][bit] = -1;
        ll sum = 0;
        for(int i=1;i<=n;i++)
        {
            if(c[i][bit]==-1)
            {
                pll res = {0,0};
                vis.clear();
                c[i][bit] = 0;
                if(!dfs(i,bit,0))
                {
                    cout<<-1;
                    return ;
                }
                for(auto x:vis)
                {
                    res.first += (1ll<<bit)*c[x][bit];
                    c[x][bit] = -1;
                } 
                vis.clear();
                c[i][bit] = 1;
                dfs(i,bit,0);
                for(auto x:vis) res.second += (1<<bit)*c[x][bit];
                sum += min(res.first,res.second);
            }
        }
        ans += sum;
    }
    cout<<ans;
}
int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    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: 15ms
memory: 80184kb

input:

3 2
1 2 1
2 3 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 12ms
memory: 80328kb

input:

3 3
1 2 1
2 3 1
1 3 1

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: 0
Accepted
time: 19ms
memory: 80400kb

input:

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

output:

58

result:

ok 1 number(s): "58"

Test #4:

score: -100
Runtime Error

input:

500 124750
1 2 31473
1 3 11597
1 4 6686
1 5 1214
1 6 14442
1 7 1042
1 8 19057
1 9 22648
1 10 24461
1 11 25714
1 12 3976
1 13 31954
1 14 7384
1 15 13988
1 16 28651
1 17 31599
1 18 8786
1 19 27068
1 20 9645
1 21 28281
1 22 11681
1 23 28897
1 24 31916
1 25 10462
1 26 23973
1 27 4615
1 28 5124
1 29 2026...

output:


result: