Submission #2034615
Source Code Expand
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
/////////////////// TYPES & MACROS ///////////////////////////////
#define INFLL 0x3fffffffffffffffLL
#define INF 0x3fffffff
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define all(x) x.begin(),x.end()
#define exist(s,e) (s.find(e)!=s.end())
#define ff first
#define ss second
#define EPS 1e7
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;
const ll MOD=1e9+7;
/////////////////// DEBUG MODE ///////////////////////////////////
#define D(x) cerr<<"DEBUG: "<<#x<<" is: "<<x<<'\n'
#define error(args...) {string _s=#args; replace(all(_s),',',' ');\
stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args);}
void err(istream_iterator<string>) { cout<<endl;}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
/////////////////// GLOBAL VARIABLES /////////////////////////////
vector<vector<int>> adj; vector<int> vis; vector<int> color;
/////////////////// FUNCTIONS ////////////////////////////////////
template<class T>
T maxx(T a, T b) {return a>b?a:b;}
template<typename T, typename... Args>
T maxx(T a, T b, Args... args){return maxx(a>b?a:b, args...);}
ll mexp(ll x, ll n, ull m){
ll res=1; while(n){if(n&1) res=(res*x)%m;n>>=1; x=((x%m)*(x%m))%m;}
return res;
}
template<class T>
T minn(T a, T b) {return a<b?a:b;}
template<typename T, typename... Args>
T minn(T a, T b, Args... args){return minn(a>b?a:b, args...);}
bool ispow2(ll x){ return x && (!(x&(x-1))); }
ld pow(ld a, int n) {
if(n == 0) return 1;if(n == 1) return a;
double t = pow(a, n/2);return t * t * pow(a, n%2);
}
bool DFS(int u){
vis[u]=1;
for(auto v: adj[u]){
if(!vis[v]) {if(!DFS(v)) return false;}
else if(vis[v]==1) return false;
}
vis[u]=2;
return true;
}
bool cycle(){
int n=adj.size();
vis.assign(n,0);
for(int i=0;i<n;++i) if(!vis[i] && !DFS(i)) return true;
return false;
}
vector<int> ts;
void toposort(int u){
vis[u]=1;
for(auto v: adj[u]) if(!vis[v]) toposort(v);
ts.eb(u);
}
/////////////////// MAIN STARTS //////////////////////////////////
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout<<fixed; cerr<<fixed;
cout<<setprecision(10); cerr<<setprecision(3);
#ifdef DEBUG
freopen("ip.txt","r",stdin); //freopen("op.txt","w",stdout);
clock_t tStart = clock();
#endif
int n,m; cin>>n>>m; adj.resize(n); vis.assign(n,0);
map<pii,int> wt;
for(int i=0;i<m;++i){
int u,v,x; cin>>u>>v>>x; --u,--v;
adj[u].eb(v); wt[{u,v}]=x;
}
if(cycle()) {cout<<"No\n"; return 0;}
else{
fill(all(vis),0);
vector<int> d(n,INF),indeg(n,0);
for(int i=0;i<n;++i){
for(auto v: adj[i]) indeg[v]++;
}
queue<int> q;
for(int i=0;i<n;++i) if(indeg[i]==0) q.push(i),d[i]=0;
while(!q.empty()){
int u=q.front(); q.pop();
for(auto v:adj[u]){
if(vis[v]==0){ q.push(v); vis[v]=1; }
if(d[v]==INF) d[v]=d[u]+wt[{u,v}];
else if(d[v] != d[u]+wt[{u,v}]) {cout<<"No\n"; return 0;}
}
}
}
cout<<"Yes\n";
#ifdef DEBUG
cerr<<"\nExecution time: "<<(((double)clock() - tStart)/CLOCKS_PER_SEC)*1000<<"ms.\n";
#endif
return 0;
}
Submission Info
Submission Time |
|
Task |
D - People on a Line |
User |
madhur4127 |
Language |
C++14 (GCC 5.4.1) |
Score |
400 |
Code Size |
3532 Byte |
Status |
AC |
Exec Time |
240 ms |
Memory |
18944 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
400 / 400 |
Status |
|
|
Set Name |
Test Cases |
Sample |
sample01.txt, sample02.txt, sample03.txt, sample04.txt, sample05.txt |
All |
01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, sample01.txt, sample02.txt, sample03.txt, sample04.txt, sample05.txt |
Case Name |
Status |
Exec Time |
Memory |
01.txt |
AC |
209 ms |
18936 KB |
02.txt |
AC |
220 ms |
18936 KB |
03.txt |
AC |
203 ms |
18816 KB |
04.txt |
AC |
240 ms |
18944 KB |
05.txt |
AC |
154 ms |
17280 KB |
06.txt |
AC |
170 ms |
15360 KB |
07.txt |
AC |
151 ms |
17280 KB |
08.txt |
AC |
155 ms |
15360 KB |
09.txt |
AC |
131 ms |
14080 KB |
10.txt |
AC |
129 ms |
14336 KB |
11.txt |
AC |
106 ms |
14080 KB |
12.txt |
AC |
123 ms |
14848 KB |
13.txt |
AC |
170 ms |
16128 KB |
14.txt |
AC |
138 ms |
14208 KB |
15.txt |
AC |
122 ms |
14848 KB |
16.txt |
AC |
136 ms |
14336 KB |
17.txt |
AC |
193 ms |
17536 KB |
18.txt |
AC |
180 ms |
16768 KB |
19.txt |
AC |
147 ms |
14848 KB |
20.txt |
AC |
148 ms |
14848 KB |
21.txt |
AC |
166 ms |
17536 KB |
22.txt |
AC |
133 ms |
14848 KB |
23.txt |
AC |
173 ms |
17536 KB |
24.txt |
AC |
124 ms |
14848 KB |
25.txt |
AC |
231 ms |
17664 KB |
26.txt |
AC |
232 ms |
17536 KB |
27.txt |
AC |
109 ms |
13952 KB |
28.txt |
AC |
136 ms |
14848 KB |
29.txt |
AC |
115 ms |
16128 KB |
30.txt |
AC |
144 ms |
14976 KB |
31.txt |
AC |
113 ms |
14336 KB |
32.txt |
AC |
149 ms |
14976 KB |
33.txt |
AC |
205 ms |
17664 KB |
34.txt |
AC |
174 ms |
17536 KB |
35.txt |
AC |
113 ms |
13568 KB |
36.txt |
AC |
128 ms |
14848 KB |
37.txt |
AC |
106 ms |
14592 KB |
38.txt |
AC |
110 ms |
14848 KB |
39.txt |
AC |
109 ms |
14976 KB |
40.txt |
AC |
148 ms |
14976 KB |
41.txt |
AC |
4 ms |
4224 KB |
42.txt |
AC |
60 ms |
8960 KB |
sample01.txt |
AC |
1 ms |
256 KB |
sample02.txt |
AC |
1 ms |
256 KB |
sample03.txt |
AC |
1 ms |
256 KB |
sample04.txt |
AC |
1 ms |
256 KB |
sample05.txt |
AC |
1 ms |
256 KB |