QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#651689 | #7860. Graph of Maximum Degree 3 | Mrlaolu | WA | 1ms | 5876kb | C++20 | 1.7kb | 2024-10-18 17:56:01 | 2024-10-18 17:56:33 |
Judging History
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;
p[a] = b;
}
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 == 0){merge(red,a,b);}
else {merge(blue,a,b);}
S.insert({a,b});
}
// cout << ans << endl;
vector<bool>vis(n+1);
int cnt = 0;
set<int>Sr,Sb;
auto dfs = [&](auto dfs,int u) -> void{
if(vis[u])return;
vis[u] = 1;
Sr.insert(find(red,red[u]));
Sb.insert(find(blue,blue[u]));
cnt++;
for(auto v : node[u]){
dfs(dfs,v);
}
};
// for(int i = 1;i <= n;++i){
// cout << red[i] << " " << blue[i] << endl;
// }
for(int i = 1;i <= n;++i){
if(vis[i] == 0){
cnt = 0;Sr.clear();Sb.clear();
dfs(dfs,i);
// cout << Sr.size() << " " << Sb.size() << endl;
if(cnt - Sr.size() >= 2 && cnt - Sb.size() >= 2)ans++;
}
}
cout << ans << endl;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int _ = 1;
// cin >> _;
while(_--){
solve();
}
}
/*
1
10 40 50
1
60 200 960
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5876kb
input:
3 4 1 2 0 1 3 1 2 3 0 2 3 1
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: 0
Accepted
time: 0ms
memory: 5688kb
input:
4 6 1 2 0 2 3 0 3 4 0 1 4 1 2 4 1 1 3 1
output:
5
result:
ok 1 number(s): "5"
Test #3:
score: 0
Accepted
time: 0ms
memory: 5836kb
input:
20 28 9 6 1 9 6 0 3 8 0 8 4 0 3 8 1 3 4 1 2 13 0 13 1 0 19 1 0 2 1 1 2 19 1 13 19 1 14 15 1 14 15 0 7 12 0 12 17 0 20 17 0 7 17 1 7 20 1 12 20 1 16 18 0 18 10 0 5 10 0 16 10 1 16 5 1 18 5 1 4 6 0 9 11 0
output:
27
result:
ok 1 number(s): "27"
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 5544kb
input:
100 150 93 23 0 23 81 0 76 81 0 93 81 1 93 76 1 23 76 1 100 65 0 65 56 0 19 56 0 100 56 1 100 19 1 65 19 1 2 98 0 2 98 1 26 63 0 63 90 0 26 63 1 26 90 1 6 11 0 11 67 0 6 11 1 6 67 1 37 89 0 89 64 0 25 64 0 37 64 1 37 25 1 89 25 1 84 10 0 10 29 0 75 29 0 84 29 1 84 75 1 10 75 1 7 70 1 7 70 0 28 92 0 ...
output:
137
result:
wrong answer 1st numbers differ - expected: '141', found: '137'