結果
問題 | No.1023 Cyclic Tour |
ユーザー |
![]() |
提出日時 | 2020-03-08 00:32:55 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 412 ms / 2,000 ms |
コード長 | 2,323 bytes |
コンパイル時間 | 1,176 ms |
コンパイル使用メモリ | 99,004 KB |
実行使用メモリ | 38,820 KB |
最終ジャッジ日時 | 2024-10-15 19:57:28 |
合計ジャッジ時間 | 15,740 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 49 |
ソースコード
#include <stdio.h> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <list> #include <iterator> #include <assert.h> #pragma warning(disable:4996) typedef long long ll; #define MIN(a, b) ((a)>(b)? (b): (a)) #define MAX(a, b) ((a)<(b)? (b): (a)) #define LINF 9223300000000000000 #define INF 2140000000 const long long MOD = 1000000007; //const long long MOD = 998244353; using namespace std; vector<set<pair<int,int> > > g; // to, directed edge vector<int> visedge; // visit flag for directed edge bool dfs(int paredge, int curr) { auto it=g[curr].begin(); while(it!=g[curr].end()) { int next=it->first; int edge=it->second; if(edge/2==paredge/2) { ++it; continue; } if(visedge[edge]) { return true; } visedge[edge]=1; if (dfs(edge, next)) { return true; } it++; g[curr].erase(make_pair(next,edge)); } return false; } void solve(int n, int m, const vector<int>& a, const vector<int>& b, const vector<int> c) { g.resize(n); visedge.resize(m*2); int i; for(i=0; i<m; i++) { g[a[i]].insert(make_pair(b[i],i*2)); // i*2 means edge i with normal direction if(c[i]==1) g[b[i]].insert(make_pair(a[i],i*2+1)); // i*2+1 means edge i with reversed direction } for(i=0; i<n; i++) { bool ret=dfs(-100, i); if(ret){ printf("Yes\n"); return; } } printf("No\n"); return; } int main(int argc, char* argv[]) { int n,m; scanf("%d%d", &n, &m); assert(n<=100000); assert(n>=2); assert(m>=1); assert(m<=200000); vector<int> a(m),b(m),c(m); int i; set<pair<pair<int,int>,int> > ss; for(i=0; i<m; i++) { scanf("%d%d%d", &a[i], &b[i],&c[i]); assert(a[i]>=1 && a[i]<=n); assert(b[i]>=1 && b[i]<=n); assert(c[i]==1 || c[i]==2); assert(a[i]!=b[i]); if(c[i]==1) { assert(a[i]<b[i]); } assert(ss.insert(make_pair(make_pair(a[i],b[i]),c[i])).second); a[i]--; b[i]--; } solve(n, m, a, b, c); return 0; }