QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#728957#6613. Bitwise Exclusive-OR Sequencenorth_hTL 94ms21372kbC++175.3kb2024-11-09 16:16:292024-11-09 16:16:31

Judging History

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

  • [2024-11-09 16:16:31]
  • 评测
  • 测评结果:TL
  • 用时:94ms
  • 内存:21372kb
  • [2024-11-09 16:16:29]
  • 提交

answer

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
#define int long long
//#define endl "\n"
using ll=long long;

//ll n;
//void solve(){
//    string s; cin >> n >> s; s=" "+s;
//    string ans;
//    for(int i=1;i<=n;i++){
//        int g[27]={0},f[27]={0},cnt=0;
////        if(i==3) cout << g[1] << g[3] << "\n";
//        for(int j=i;j;j--) if(!f[s[j]-'a'+1]){
//            g[s[j]-'a'+1]=cnt++;
//            f[s[j]-'a'+1]=1;
////            if(i==3) cout << s[j]-'a'+1 << "\n";
//            if(cnt==26) break;
//        }
////        if(i==3) cout << g[1] << g[3] << "\n";
//        string ts;
//        for(int j=1;j<=i;j++) ts+=(char)('a'+g[s[j]-'a'+1]);
//        ans=max(ts,ans);
////        cout << ts << "\n";
//    }
//    cout << ans;
//}

const int N=1e5+10;
//int dis[N];
//void bfs(){
//    memset(dis,-1,sizeof dis);
//    queue<int>qu;
//    qu.push(0);
//    dis[0]=0;
//    while(!qu.empty()){
//        int num=qu.front(); qu.pop();
//        int a[5]={0};
//        int tnum=num;
//        for(int i=4;i;i--) a[i]=tnum%10,tnum/=10;
//        for(int i=1;i<=4;i++){
//            for(int j=i;j<=4;j++){
//                int num1=0,num2=0;
//                for(int k=1;k<=4;k++){
//                    if(k>=i && k<=j) num1=num1*10+(a[k]+1)%10,num2=num2*10+(a[k]+9)%10;
//                    else num1=num1*10+a[k],num2=num2*10+a[k];
//                }
//                if(dis[num1]==-1) dis[num1]=dis[num]+1,qu.push(num1);
//                if(dis[num2]==-1) dis[num2]=dis[num]+1,qu.push(num2);
//            }
//        }
//    }
//}
//void solve(){
//    bfs();
//    int tc; cin >> tc;
//    while(tc--){
//        string s,tos; cin >> s >> tos;
//        int a[5]={0},b[5]={0};
//        for(int i=0;i<4;i++) a[i+1]=s[i]-'0'+1,b[i+1]=tos[i]-'0'+1;
//        int num=0;
//        for(int i=1;i<=4;i++) num=num*10+(b[i]-a[i]+10)%10;
//        cout << dis[num] << "\n";
////        cout << num << "\n";
//    }
////    cout << dis[7];
//}

int n, m;
vector<pair<int, int>> g[N];
int x[N * 2], y[N * 2], z[N * 2];
int vis[N], vi[N];
bool ok;

int bfs(int u) {
    vector<int> d(n + 1);queue<int> q;
    q.push(u);vis[u] = 1;d[u] = 0;
    while (q.size()) {
        auto t = q.front(); q.pop();
        for (auto [x, y] : g[t]) {
            if (vis[x]) {
                if (y == 1 && d[x] == d[t]) {
                    ok = true;
                    return -1;
                } else if (y == 0 && d[x] != d[t]) {
                    ok = true;
                    return -1;
                }
            } else {
                if (y == 1) d[x] = d[t] ^ 1;
                else d[x] = d[t];
                q.push(x);
                vis[x] = 1;
            }
        }
    }
    int sum = 0;
    for (int i = 1; i <= n; i ++) sum += d[i];
    return sum;
}

int bfs1(int u) {
    vector<int> d(n + 1);queue<int> q;
    q.push(u);vi[u] = 1;d[u] = 1;
    while (q.size()) {
        auto t = q.front();q.pop();
        for (auto [x, y] : g[t]) {
            if (vi[x]) {
                if (y == 1 && d[x] == d[t]) {
                    ok = true;
                    return -1;
                } else if (y == 0 && d[x] != d[t]) {
                    ok = true;
                    return -1;
                }
            } else {
                if (y == 1) d[x] = d[t] ^ 1;
                else d[x] = d[t];
                q.push(x);
                vi[x] = 1;
            }
        }
    }
    int sum = 0;
    for (int i = 1; i <= n; i ++) sum += d[i];
    return sum;
}

void solve() {
    cin >> n >> m;
//    map<pair<int, int>, int> mp;
    unordered_map<int, unordered_map<int, int>> mp;
    for (int i = 1; i <= m; i ++) {
        int u, v, w; cin >> u >> v >> w;
        x[i] = u, y[i] = v, z[i] = w;
//        if (!mp.count({u, v})) mp[{u, v}] = w;
//        else if (mp[{u, v}] != w) ok = true;
            if (mp[u][v] == 0) mp[u][v] = w;
            else if (mp[u][v] != w) ok = true;
    }
    if (ok) {
        cout << -1 << '\n';
        return ;
    }
    int ans = 0;
    for (int i = 0; i < 30; i ++) {
        for (int j = 1; j <= n; j ++) g[j].clear();
        for (int j = 1; j <= m; j ++) {
            int u = x[j], v = y[j], w = z[j];
            if (w >> i & 1) {
                g[u].push_back({v, 1});
                g[v].push_back({u, 1});
            } else {
                g[u].push_back({v, 0});
                g[v].push_back({u, 0});
            }
        }
//        for (int j = 1; j <= n; j ++) {
//            cout << j << ":\n";
//            for (auto [x, y] : g[j]) cout << x << '|' << y << '\n';
//        }
        for (int j = 1; j <= n; j ++) vi[j] = vis[j] = 0;
        for (int j = 1; j <= n; j ++) {
            if (!vis[j]) {
//                cout << bfs(j) << ' ' << bfs1(j) << '\n';
                ans += min(bfs(j), bfs1(j)) * (1ll << i);
                if (ok) {cout << -1 << '\n'; return ;}
            }
        }
//        cout << i << ' ' << ans << ' ' << ok << endl;
    }
    if (ok) cout << -1 << '\n';
    else cout << ans << '\n';
}

int32_t 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: 11040kb

input:

3 2
1 2 1
2 3 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 10240kb

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

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: 0
Accepted
time: 94ms
memory: 20240kb

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:

8041745

result:

ok 1 number(s): "8041745"

Test #5:

score: 0
Accepted
time: 91ms
memory: 21372kb

input:

500 124750
1 2 3902
1 3 9006
1 4 2914
1 5 8753
1 6 2395
1 7 21406
1 8 14552
1 9 25834
1 10 28282
1 11 9684
1 12 11347
1 13 20545
1 14 16324
1 15 16951
1 16 11594
1 17 5035
1 18 17726
1 19 831
1 20 23194
1 21 7693
1 22 6147
1 23 315
1 24 32755
1 25 17078
1 26 11348
1 27 9587
1 28 21015
1 29 881
1 30 ...

output:

7803950

result:

ok 1 number(s): "7803950"

Test #6:

score: -100
Time Limit Exceeded

input:

100000 200000
82262 26109 1005476194
43106 86715 475153289
59086 60577 507254441
71498 80384 186530379
99676 3003 289537598
30772 72897 345346447
12686 87447 896623879
12520 27709 26442442
82734 20830 967590473
13380 76164 927495776
25723 55377 89078582
7173 86993 669894679
37790 94846 331905713
365...

output:


result: