i want u to act as the most skilled competetive programmer, u take time to think and…
- User
i want u to act as the most skilled competetive programmer, u take time to think and check if ur solution works and match up with the example tescase, u need to think thoroughly since u r in a contest. use need to use this template, make sure u have lowercase rlly short variable names and NO COMMENTS (in the code, at all) and since ur using this template always use int and not long long, PLS REVALUATE UR CODE AND MAKE SURE IT MAKES SURE IT MATCHES EVERY SINGLE SAMPLE INPUT AND OUTPUT GIVEN, THINK MULTIPLE TIMES BEFORE GIVING UR FINAL ANSWER AS THIS IS A CONTEST: #include <bits/stdc++.h> #define int long long using namespace std;
signed main() { cin.tie(0); ios_base::sync_with_stdio(false); }
Time Limit: 2 sec / Memory Limit: 1024 MB
× It is prohibited to directly input all or part of the information issued as problems in ongoing AtCoder contests into software. Please refer to this post for details.
Score : 400 points
Problem Statement You are given a directed graph with N vertices and M edges. The j-th directed edge goes from vertex u j to vertex v j and has a weight of w j .
Find one way to write an integer between −10 18 and 10 18 , inclusive, to each vertex such that the following condition is satisfied.
Let x i be the value written on vertex i. For all edges j=1,2,…,M, it holds that x v j
−x u j
=w j . It is guaranteed that at least one such assignment exists for the given input.
Constraints 2≤N≤2×10 5
1≤M≤min{2×10 5 ,N(N−1)/2} 1≤u j ,v j ≤N u j
=v j
If i =j, then (u i ,v i ) =(u j ,v j ) and (u i ,v i ) =(v j ,u j ) ∣w j ∣≤10 9
All input values are integers. There exists at least one assignment satisfying the conditions. Input The input is given from Standard Input in the following format:
N M u 1
v 1
w 1
u 2
v 2
w 2
⋮ u M
v M
w M
Output Let x i be the integer written on vertex i. Print x 1 ,x 2 ,…,x N in this order, separated by spaces, on a single line. If there are multiple solutions, you may print any of them.
Sample Input 1 Copy 3 3 1 2 2 3 2 3 1 3 -1 Sample Output 1 Copy 3 5 2 By setting x=(3,5,2), we have x 2 −x 1 =w 1 =2, x 2 −x 3 =w 2 =3, x 3 −x 1 =w 3 =−1, satisfying the conditions.
For example, x=(−1,1,−2) is also a valid answer.
Sample Input 2 Copy 4 2 2 1 5 3 4 -3 Sample Output 2 Copy 5 0 6 3 For example, x=(0,−5,4,1) and x=(5,0,4,1) are also valid answers.
Sample Input 3 Copy 5 7 2 1 18169343 3 1 307110901 4 1 130955934 2 3 -288941558 2 5 96267410 5 3 -385208968 4 3 -176154967 Sample Output 3 Copy 200401298 182231955 -106709603 69445364 278499365
- Assistant
#include <bits/stdc++.h> #define int long long using namespace std; signed main() { cin.tie(0); ios_base::sync_with_stdio(false); int n,m; cin>>n>>m; vector<vector<pair<int,int>>> adj(n+1); for(int i=0;i<m;i++){int u,v,w;cin>>u>>v>>w;adj[u].emplace_back(v,w);} vector<int> x(n+1,0); vector<bool> vis(n+1, false); for(int i=1;i<=n;i++) if(!vis[i]){ vis[i]=true; x[i]=0; queue<int> q; q.push(i); while(!q.empty()){ int u=q.front(); q.pop(); for(auto &[v,w]: adj[u]){ if(!vis[v]){ x[v]=x[u]+w; vis[v]=true; q.push(v); } } } } for(int i=1;i<=n;i++) cout<<x[i]<<(i<n?" ":"\n"); }