Skip to content

Instantly share code, notes, and snippets.

@ctylim
Created November 3, 2017 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctylim/c0fab0221d7a14cdc7569c29e912f650 to your computer and use it in GitHub Desktop.
Save ctylim/c0fab0221d7a14cdc7569c29e912f650 to your computer and use it in GitHub Desktop.
DDCC2017 Final C
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define _repargs(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _repargs(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p = 0) {
if(p) cout << fixed << setprecision(p) << a << "\n";
else cout << a << "\n";
}
// end of template
ll inf = 1LL << 61;
struct node {int to; ll cost;};
vector<vector<node>> G;
vector<vector<node>> rG;
vector<ll> dist;
int to = 0;
bool ok = true;
void dfs(int cur) {
for(node n: G[cur]) {
if(to == n.to) continue;
if(dist[n.to] == inf) {
dist[n.to] = dist[cur] + n.cost;
dfs(n.to);
}
else {
if(dist[n.to] != dist[cur] + n.cost) ok = false;
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
// source code
int N, M;
cin >> N >> M;
G.resize(N);
rG.resize(N);
rep(i, M) {
int a, b;
ll c;
cin >> a >> b >> c;
a--;
b--;
G[a].pb({b, c});
rG[b].pb({a, c});
}
rep(i, N) {
dist.assign(N, inf);
dist[i] = 0;
ok = true;
to = i;
dfs(i);
if(ok) {
int cnt = 0;
for (node n: rG[i]) {
if(dist[n.to] + n.cost != 0) cnt++;
}
if(cnt <= 1) {
output("Yes");
return 0;
}
}
}
output("No");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment