結果
| 問題 |
No.2664 Prime Sum
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2024-03-08 21:19:47 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 54 ms / 2,000 ms |
| コード長 | 2,497 bytes |
| コンパイル時間 | 4,036 ms |
| コンパイル使用メモリ | 252,304 KB |
| 最終ジャッジ日時 | 2025-02-20 02:05:07 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 37 |
ソースコード
#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef long double ld;
typedef pair<int, int> P;
ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const dynamic_modint<m>& a) {os << a.val(); return os;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;}
template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second; return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;}
template<typename T> void chmin(T& a, T b){a = min(a, b);}
template<typename T> void chmax(T& a, T b){a = max(a, b);}
// Weighted Union Find
// O(alpha(N))
// thanks for "https://ei1333.github.io/luzhiled/snippets/structure/union-find.html"
template<typename T>
struct WeightedUnionFind {
vector<int> data;
vector<T> ws;
WeightedUnionFind() {}
WeightedUnionFind(int sz) : data(sz, -1), ws(sz) {}
int root(int k){
if(data[k] < 0) return k;
auto par = root(data[k]);
ws[k] += ws[data[k]];
return data[k] = par;
}
T weight(int t) {
root(t);
return ws[t];
}
bool same(int x, int y){
x = root(x), y = root(y);
if(x == y) return true;
else return false;
}
bool unite(int x, int y, T w) {
w += weight(x);
w -= weight(y);
x = root(x), y = root(y);
if(x == y) return false;
if(data[x] > data[y]) {
swap(x, y);
w *= -1;
}
data[x] += data[y];
data[y] = x;
ws[y] = w;
return true;
}
T diff(int x, int y) {
return weight(y) - weight(x);
}
};
int main(){
int n, m;
cin >> n >> m;
WeightedUnionFind<int> uf(n);
rep(i, m){
int u, v;
cin >> u >> v;
u--; v--;
if(uf.same(u, v)){
int w = uf.diff(u, v);
if(abs(w) % 2 == 0){
cout << "No\n";
return 0;
}
}else{
uf.unite(u, v, 1);
}
}
cout << "Yes\n";
return 0;
}