QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#656618#7860. Graph of Maximum Degree 3MrlaoluWA 1ms4020kbC++203.1kb2024-10-19 13:26:132024-10-19 13:26:13

Judging History

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

  • [2024-10-19 13:26:13]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4020kb
  • [2024-10-19 13:26:13]
  • 提交

answer

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

using LL = long long;

const int N = 301000;

int n, m;
int a[N];

int red[N],blue[N];

int find(int p[], int x){
    if(x != p[x]) p[x] = find(p, p[x]);
    return p[x];
}

void merge(int p[], int x, int y){
    int a = find(p, x), b = find(p, y);
    if(a == b) return;
    if(a > b)swap(a,b);
    p[b] = a;
}
using PII = pair<int,int>;
void solve(){

    cin >> n >> m;
    int res = n;
    if(n == 1){
        cout << 1 << "\n";
        return;
    }
    else if(n == 2){
        if(m == 2) res ++;
        cout << res << "\n";
        return;
    }

    vector<vector<int>>node(n+1);
    for(int i = 1;i <= n;++i){
        red[i] = i;
        blue[i] = i;
    }
    int ans = n;
    set<PII>S;

    for(int i = 1; i <= m; i ++){
        int a,b,c;
        cin >> a >> b >> c;
        if(a > b)swap(a,b);
        if(S.count({a,b}))ans++;
        node[a].push_back(b);
        node[b].push_back(a);
        if(c == 1){merge(red,a,b);}
        else {merge(blue,a,b);}
        S.insert({a,b});
    }
    // cout << ans << endl;
    vector<bool>vis(n+1);
    int cnt = 0;
    map<int,vector<int>>pred;
    map<int,vector<int>>pblue;
    auto dfs = [&](auto dfs,int u) -> void{
        if(vis[u])return;
        vis[u] = 1;
        pred[find(red,u)].push_back(u);
        pblue[find(blue,u)].push_back(u);
        cnt++;
        for(auto v : node[u]){
            dfs(dfs,v);
        }
    };
    vector<bool>use(n+1);
    for(int i = 1;i <= n;++i){
        if(vis[i] == 0){
            cnt = 0;pred.clear();pblue.clear();
            dfs(dfs,i);
            for(auto [key,vec] : pred){
                if(vec.size() < 3)continue;
                int yes = 1;
                int p = find(blue,*vec.begin());
                for(auto it : vec){
                    if(find(blue,it) != p || use[it]){yes = 0;break;}
                }
                if(yes){
//                    cout << p << endl;
                    for(auto it : vec)use[it] = 1;
                    ans++;
                }
            }
            for(auto [key,vec] : pblue){
                if(vec.size() < 3)continue;
                int yes = 1;
                int p = find(red,*vec.begin());
                for(auto it : vec){
                    if(find(red,it) != p || use[it]){yes = 0;break;}
                }
                if(yes){ans++;
//                    cout << p << endl;
                    for(auto it : vec)use[it] = 1;
                }
            }
        }
    }
    cout << ans << endl;
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    freopen("in.txt","r",stdin);
    int _ = 1;
    // cin >> _;
    while(_--){
        solve();
    }
}

/*
1
10 40 50

1
60 200 960



13 19
1 2 0
1 2 1
1 3 1
2 3 0
4 3 1
4 5 1
5 6 1
5 7 0
6 7 0
6 7 1
4 8 0
8 9 1
8 10 0
9 10 1
9 10 0
11 12 1
11 13 0
12 13 0
12 13 1


7 9
1 2 0
1 2 1
1 3 1
2 3 0
4 3 1
4 5 0
5 6 1
5 7 0
6 7 1
 */

詳細信息

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 4020kb

input:

3 4
1 2 0
1 3 1
2 3 0
2 3 1

output:

0

result:

wrong answer 1st numbers differ - expected: '5', found: '0'