結果

問題 No.1023 Cyclic Tour
ユーザー cn_449
提出日時 2020-04-11 23:19:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,650 bytes
コンパイル時間 1,227 ms
コンパイル使用メモリ 114,876 KB
実行使用メモリ 14,140 KB
最終ジャッジ日時 2024-09-19 16:24:33
合計ジャッジ時間 6,400 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 47 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#define int long long
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;
typedef complex<ld> com;
constexpr ll INF = 1000000000000000000;
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }
class unionfind {
vector<int> par;
vector<int> sz;
public:
unionfind(int n) {
par = vector<int>(n);
for (int i = 0; i < n; i++) par[i] = i;
sz = vector<int>(n, 1);
}
int find(int x) {
if (par[x] == x) return x;
else return par[x] = find(par[x]);
}
int si(int x) { return sz[find(x)]; }
bool same(int x, int y) { return find(x) == find(y); }
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (sz[x] < sz[y]) {
par[x] = y;
sz[y] += sz[x];
}
else {
par[y] = x;
sz[x] += sz[y];
}
}
};
vector<vector<int>> graph(100010, vector<int>());
vector<bool> vis(100010);
bool ans = false;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int n, m;
cin >> n >> m;
unionfind uni(n);
vector<P> edge;
rep(i, m) {
int u, v;
cin >> u >> v;
u--; v--;
int c;
cin >> c;
if (c == 1){
if(uni.same(u, v)){
cout << "Yes" << endl;
return 0;
}
uni.unite(u, v);
}
else edge.pb(P(u, v));
}
vector<int> deg(n);
for (P p : edge) {
if (uni.find(p.first) == uni.find(p.second)) {
cout << "Yes" << endl;
return 0;
}
graph[uni.find(p.first)].pb(uni.find(p.second));
deg[uni.find(p.second)]++;
}
queue<int> que;
rep(i, n) {
int r = uni.find(i);
if (!vis[r] && deg[r] == 0) {
vis[r] = true;
que.push(r);
}
}
while (!que.empty()) {
int v = que.front(); que.pop(); vis[v] = true;
for (int i : graph[v]) {
vis[i] = true;
/*if (deg[i] == 0) {
ans = true;
break;
}*/
//else {
deg[i]--;
if (deg[i] == 0) que.push(i);
//}
}
}
rep(i, n) {
if (!vis[uni.find(i)]) ans = true;
}
cout << (ans ? "Yes" : "No") << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0