#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define nmax 100001
vector <int> edges[nmax], price[nmax], arbore[nmax], pricearb[nmax], v;
int daddy[nmax], type[nmax], paritate[nmax], comp[nmax], muchii[nmax];
double modificat[nmax];
const int inf = 1e8;
int find_daddy( int a ) {
int x;
if( daddy[a] == a )
return a;
x = find_daddy(daddy[a]);
daddy[a] = x;
return x;
}
void marry_daddy( int a, int b ) {
int x, y;
x = find_daddy( a );
y = find_daddy( b );
daddy[x] = y;
}
void dfs( int vf, int parent ) {
int i, fiu;
v.push_back(type[vf] * paritate[vf]);
comp[find_daddy(vf)]++;
muchii[find_daddy(vf)] += edges[vf].size();
//cout << vf << "\n";
for( i = 0; i < arbore[vf].size(); i++ ) {
fiu = arbore[vf][i];
if( fiu == parent )
continue;
type[fiu] = pricearb[vf][i] - type[vf];
paritate[fiu] = -paritate[vf];
//cout << fiu << " " << type[fiu] << " " << pricearb[vf][i] << " " << type[vf] << "\n";
dfs( fiu, vf );
}
}
void afisare( int x ) {
if( (x + inf) % 2 == 0 )
cout << x / 2 << " ";
else
cout << (x - 1) / 2 << ".5 ";
}
int main() {
int n, m, i, a, b, c, j;
bool ok = true;
cin >> n >> m;
for( i = 1; i <= n; i++ )
daddy[i] = i;
for( i = 0; i < m; i++ ) {
cin >> a >> b >> c;
edges[a].push_back( b );
edges[b].push_back( a );
price[a].push_back( c );
price[b].push_back( c );
if( find_daddy(a) != find_daddy(b) ) {
marry_daddy( a, b );
arbore[a].push_back(b);
arbore[b].push_back(a);
pricearb[a].push_back(c);
pricearb[b].push_back(c);
}
}
for( i = 1; i <= n; i++ ) {
if( find_daddy(i) == i ) {
paritate[i] = 1;
dfs( i, 0 );
if( 2 * comp[i] == muchii[i] + 2 ) {
//cout << ".";
sort(v.begin(), v.end());
//for( j = 0; j < v.size(); j++ )
// cout << v[j] << " ";
//cout << "\n";
modificat[find_daddy(i)] = -v[(v.size() - 1)/ 2];
}
v.clear();
}
}
//for( i = 1; i <= n; i++ )
// cout << type[i] << " ";
for( i = 1; i <= n; i++ ) {
for( j = 0; j < edges[i].size(); j++ ) {
if( type[i] + type[edges[i][j]] + (paritate[i] + paritate[edges[i][j]]) * modificat[find_daddy(i)] != price[i][j] ) {
if( modificat[find_daddy(i)] == 0 && paritate[i] == paritate[edges[i][j]] )
modificat[find_daddy(i)] = (((double)price[i][j] - type[i] - type[edges[i][j]]) / 2) * paritate[i];
else ok = false;
}
}
}
//cout << "\n";
//for( i = 1; i <= n; i++ )
// cout << type[i] + paritate[i] * modificat[find_daddy(i)] << " ";
if( !ok ) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
for( i = 1; i <= n; i++ )
cout << type[i] + paritate[i] * modificat[find_daddy(i)] << " ";
return 0;
}
Compilation message
Graph.cpp: In function 'void dfs(int, int)':
Graph.cpp:32:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for( i = 0; i < arbore[vf].size(); i++ ) {
| ~~^~~~~~~~~~~~~~~~~~~
Graph.cpp: In function 'int main()':
Graph.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for( j = 0; j < edges[i].size(); j++ ) {
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9820 KB |
answer = YES |
2 |
Correct |
4 ms |
9820 KB |
answer = YES |
3 |
Correct |
4 ms |
9820 KB |
answer = YES |
4 |
Correct |
4 ms |
9820 KB |
answer = NO |
5 |
Correct |
4 ms |
9820 KB |
answer = YES |
6 |
Correct |
4 ms |
9816 KB |
answer = YES |
7 |
Correct |
5 ms |
9820 KB |
answer = YES |
8 |
Correct |
4 ms |
9820 KB |
answer = YES |
9 |
Correct |
4 ms |
9820 KB |
answer = NO |
10 |
Correct |
5 ms |
9816 KB |
answer = YES |
11 |
Correct |
4 ms |
9820 KB |
answer = YES |
12 |
Correct |
4 ms |
9820 KB |
answer = NO |
13 |
Correct |
4 ms |
9840 KB |
answer = YES |
14 |
Correct |
4 ms |
9820 KB |
answer = YES |
15 |
Correct |
4 ms |
9820 KB |
answer = YES |
16 |
Correct |
4 ms |
9820 KB |
answer = YES |
17 |
Correct |
4 ms |
9820 KB |
answer = YES |
18 |
Correct |
4 ms |
9820 KB |
answer = YES |
19 |
Correct |
4 ms |
9820 KB |
answer = YES |
20 |
Correct |
4 ms |
9820 KB |
answer = YES |
21 |
Correct |
4 ms |
9700 KB |
answer = YES |
22 |
Correct |
5 ms |
9816 KB |
answer = NO |
23 |
Correct |
4 ms |
9816 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9820 KB |
answer = YES |
2 |
Correct |
4 ms |
9820 KB |
answer = YES |
3 |
Correct |
4 ms |
9820 KB |
answer = YES |
4 |
Correct |
4 ms |
9820 KB |
answer = NO |
5 |
Correct |
4 ms |
9820 KB |
answer = YES |
6 |
Correct |
4 ms |
9816 KB |
answer = YES |
7 |
Correct |
5 ms |
9820 KB |
answer = YES |
8 |
Correct |
4 ms |
9820 KB |
answer = YES |
9 |
Correct |
4 ms |
9820 KB |
answer = NO |
10 |
Correct |
5 ms |
9816 KB |
answer = YES |
11 |
Correct |
4 ms |
9820 KB |
answer = YES |
12 |
Correct |
4 ms |
9820 KB |
answer = NO |
13 |
Correct |
4 ms |
9840 KB |
answer = YES |
14 |
Correct |
4 ms |
9820 KB |
answer = YES |
15 |
Correct |
4 ms |
9820 KB |
answer = YES |
16 |
Correct |
4 ms |
9820 KB |
answer = YES |
17 |
Correct |
4 ms |
9820 KB |
answer = YES |
18 |
Correct |
4 ms |
9820 KB |
answer = YES |
19 |
Correct |
4 ms |
9820 KB |
answer = YES |
20 |
Correct |
4 ms |
9820 KB |
answer = YES |
21 |
Correct |
4 ms |
9700 KB |
answer = YES |
22 |
Correct |
5 ms |
9816 KB |
answer = NO |
23 |
Correct |
4 ms |
9816 KB |
answer = NO |
24 |
Correct |
4 ms |
9820 KB |
answer = YES |
25 |
Correct |
5 ms |
9820 KB |
answer = YES |
26 |
Correct |
4 ms |
9820 KB |
answer = YES |
27 |
Correct |
4 ms |
9820 KB |
answer = YES |
28 |
Correct |
4 ms |
9820 KB |
answer = YES |
29 |
Incorrect |
7 ms |
9820 KB |
participant answer is larger than the answer of jury |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9820 KB |
answer = YES |
2 |
Correct |
4 ms |
9820 KB |
answer = YES |
3 |
Correct |
4 ms |
9820 KB |
answer = YES |
4 |
Correct |
4 ms |
9820 KB |
answer = NO |
5 |
Correct |
4 ms |
9820 KB |
answer = YES |
6 |
Correct |
4 ms |
9816 KB |
answer = YES |
7 |
Correct |
5 ms |
9820 KB |
answer = YES |
8 |
Correct |
4 ms |
9820 KB |
answer = YES |
9 |
Correct |
4 ms |
9820 KB |
answer = NO |
10 |
Correct |
5 ms |
9816 KB |
answer = YES |
11 |
Correct |
4 ms |
9820 KB |
answer = YES |
12 |
Correct |
4 ms |
9820 KB |
answer = NO |
13 |
Correct |
4 ms |
9840 KB |
answer = YES |
14 |
Correct |
4 ms |
9820 KB |
answer = YES |
15 |
Correct |
4 ms |
9820 KB |
answer = YES |
16 |
Correct |
4 ms |
9820 KB |
answer = YES |
17 |
Correct |
4 ms |
9820 KB |
answer = YES |
18 |
Correct |
4 ms |
9820 KB |
answer = YES |
19 |
Correct |
4 ms |
9820 KB |
answer = YES |
20 |
Correct |
4 ms |
9820 KB |
answer = YES |
21 |
Correct |
4 ms |
9700 KB |
answer = YES |
22 |
Correct |
5 ms |
9816 KB |
answer = NO |
23 |
Correct |
4 ms |
9816 KB |
answer = NO |
24 |
Correct |
4 ms |
9820 KB |
answer = YES |
25 |
Correct |
5 ms |
9820 KB |
answer = YES |
26 |
Correct |
4 ms |
9820 KB |
answer = YES |
27 |
Correct |
4 ms |
9820 KB |
answer = YES |
28 |
Correct |
4 ms |
9820 KB |
answer = YES |
29 |
Incorrect |
7 ms |
9820 KB |
participant answer is larger than the answer of jury |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9820 KB |
answer = YES |
2 |
Correct |
4 ms |
9820 KB |
answer = YES |
3 |
Correct |
4 ms |
9820 KB |
answer = YES |
4 |
Correct |
4 ms |
9820 KB |
answer = NO |
5 |
Correct |
4 ms |
9820 KB |
answer = YES |
6 |
Correct |
4 ms |
9816 KB |
answer = YES |
7 |
Correct |
5 ms |
9820 KB |
answer = YES |
8 |
Correct |
4 ms |
9820 KB |
answer = YES |
9 |
Correct |
4 ms |
9820 KB |
answer = NO |
10 |
Correct |
5 ms |
9816 KB |
answer = YES |
11 |
Correct |
4 ms |
9820 KB |
answer = YES |
12 |
Correct |
4 ms |
9820 KB |
answer = NO |
13 |
Correct |
4 ms |
9840 KB |
answer = YES |
14 |
Correct |
4 ms |
9820 KB |
answer = YES |
15 |
Correct |
4 ms |
9820 KB |
answer = YES |
16 |
Correct |
4 ms |
9820 KB |
answer = YES |
17 |
Correct |
4 ms |
9820 KB |
answer = YES |
18 |
Correct |
4 ms |
9820 KB |
answer = YES |
19 |
Correct |
4 ms |
9820 KB |
answer = YES |
20 |
Correct |
4 ms |
9820 KB |
answer = YES |
21 |
Correct |
4 ms |
9700 KB |
answer = YES |
22 |
Correct |
5 ms |
9816 KB |
answer = NO |
23 |
Correct |
4 ms |
9816 KB |
answer = NO |
24 |
Correct |
4 ms |
9820 KB |
answer = YES |
25 |
Correct |
5 ms |
9820 KB |
answer = YES |
26 |
Correct |
4 ms |
9820 KB |
answer = YES |
27 |
Correct |
4 ms |
9820 KB |
answer = YES |
28 |
Correct |
4 ms |
9820 KB |
answer = YES |
29 |
Incorrect |
7 ms |
9820 KB |
participant answer is larger than the answer of jury |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9820 KB |
answer = YES |
2 |
Correct |
4 ms |
9820 KB |
answer = YES |
3 |
Correct |
4 ms |
9820 KB |
answer = YES |
4 |
Correct |
4 ms |
9820 KB |
answer = NO |
5 |
Correct |
4 ms |
9820 KB |
answer = YES |
6 |
Correct |
4 ms |
9816 KB |
answer = YES |
7 |
Correct |
5 ms |
9820 KB |
answer = YES |
8 |
Correct |
4 ms |
9820 KB |
answer = YES |
9 |
Correct |
4 ms |
9820 KB |
answer = NO |
10 |
Correct |
5 ms |
9816 KB |
answer = YES |
11 |
Correct |
4 ms |
9820 KB |
answer = YES |
12 |
Correct |
4 ms |
9820 KB |
answer = NO |
13 |
Correct |
4 ms |
9840 KB |
answer = YES |
14 |
Correct |
4 ms |
9820 KB |
answer = YES |
15 |
Correct |
4 ms |
9820 KB |
answer = YES |
16 |
Correct |
4 ms |
9820 KB |
answer = YES |
17 |
Correct |
4 ms |
9820 KB |
answer = YES |
18 |
Correct |
4 ms |
9820 KB |
answer = YES |
19 |
Correct |
4 ms |
9820 KB |
answer = YES |
20 |
Correct |
4 ms |
9820 KB |
answer = YES |
21 |
Correct |
4 ms |
9700 KB |
answer = YES |
22 |
Correct |
5 ms |
9816 KB |
answer = NO |
23 |
Correct |
4 ms |
9816 KB |
answer = NO |
24 |
Correct |
4 ms |
9820 KB |
answer = YES |
25 |
Correct |
5 ms |
9820 KB |
answer = YES |
26 |
Correct |
4 ms |
9820 KB |
answer = YES |
27 |
Correct |
4 ms |
9820 KB |
answer = YES |
28 |
Correct |
4 ms |
9820 KB |
answer = YES |
29 |
Incorrect |
7 ms |
9820 KB |
participant answer is larger than the answer of jury |
30 |
Halted |
0 ms |
0 KB |
- |