Submission #00016


ソースコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct UnionFind {
vector<int> par;
vector<int> size;
UnionFind(int n) {
par.resize(n + 1, -1);//親保存配列
size.resize(n + 1, 1);//木のサイズ
}
int root(int x) {//親調べ
while (1) {
if (par[x] == -1) {
return(x);
}
x = par[x];
}
}
void unite(int u, int v) {//木の接続
int RootU = root(u), RootV = root(v);
if (RootU == RootV) {
return;
}
if (size[RootU] < size[RootV]) {
par[RootU] = RootV;
size[RootV] += size[RootU];
} else {
par[RootV] = RootU;
size[RootU] += size[RootV];
}
return;
}
};
main(){
int n,m;cin>>n>>m;
int a[m],b[m],c[m];
UnionFind g(n+1);
map<int,bool>ma;
vector<vector<pair<int,int>>>s(n+1,vector<pair<int,int>>(0));
for(int i=0;i<m;i++){
cin>>a[i]>>b[i]>>c[i];
g.unite(a[i],b[i]);
s[a[i]].push_back({b[i],c[i]});
s[b[i]].push_back({a[i],-c[i]});
}
vector<int>t(n+1,LONG_LONG_MAX);
for(int i=1;i<=n;i++){
if(!ma.count(g.root(i))){
ma[g.root(i)]=true;
t[i]=0;
queue<int>q;q.push(i);
while(!q.empty()){
int now=q.front();q.pop();
//cout<<i<<" "<<now<<"\n";
for(int j=0;j<s[now].size();j++){
if(t[s[now][j].first]==LONG_LONG_MAX){
t[s[now][j].first] = t[now] + s[now][j].second;
q.push(s[now][j].first);
}else{
if(t[s[now][j].first] != t[now] + s[now][j].second){
cout<<"Yes\n";
return(0);
}
}
}
}
}
}
cout<<"No\n";
}

ステータス

項目 データ
問題 0009 - Position Consistency Check
ユーザー名 ei2326
投稿日時 2024-08-09 11:16:22
言語 C++17
状態 Accepted
得点 20
ソースコード長 2050 Byte
最大実行時間 122 ms
最大メモリ使用量 16604 KB

セット

セット 得点 Cases
1 ALL 20 / 20 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in1.txt AC 20 ms 604 KB
1
in2.txt AC 17 ms 700 KB
1
in3.txt AC 22 ms 540 KB
1
in4.txt AC 24 ms 508 KB
1
in5.txt AC 18 ms 472 KB
1
in6.txt AC 122 ms 16544 KB
1
in7.txt AC 91 ms 12868 KB
1
in8.txt AC 114 ms 16604 KB
1
in9.txt AC 107 ms 12804 KB
1