//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;
void solve(){
int n, m; cin >> n >> m;
vector<vector<T>>g(n+1);
for (int i = 0; i<m; i++) {
int a, b, c; cin >> a >> b >> c;
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
vector<T>poly(n+1);
vector<bool>vis(n+1);
long double ans = oo;
long double eps = 1e-9;
function<void(int, int)>dfs = [&](int v, int pa){
vis[v] = 1;
for (auto [x, c]: g[v]){
if (x == pa) continue;
if (!vis[x]){
poly[x].first = -poly[v].first;
poly[x].second = c - poly[v].first;
dfs(x, v);
} else {
//solve the equation
//curr.first * x + curr.second = poly[x].first * x + poly[x].second
T curr = {-poly[v].first, c - poly[v].second};
debug(v, x, curr, poly[x]);
if (curr.first == poly[x].first) {
if (curr.second != poly[x].second){
cout << "NO\n";
exit(0);
}
continue;
}
long double now = (long double)(poly[x].second - curr.second)/(curr.first - poly[x].first);
debug(x, v, now);
if (ans == oo) ans = now;
else {
if (abs(now - ans) > eps){
cout << "NO\n";
exit(0);
}
}
}
}
};
poly[1].first = 1;
dfs(1, 1);
debug(poly);
cout << fixed << setprecision(10);
vector<long double>ret(n+1);
auto solve = [&](long double curr){
vis.assign(n+1, 0);
function<void(int)>restore = [&](int v){
vis[v] = 1;
for (auto [x, c]: g[v]){
if (!vis[x]){
ret[x] = (long double)c - ret[v];
restore(x);
}
}
};
ret[1] = curr;
restore(1);
};
if (ans == oo){
vector<long double>check = {-2, -1, 0, -0.5, 0.5, 1, 2};
long double mn = oo;
for (auto x: check){
solve(x);
long double s = 0;
for (int i = 1; i<=n; i++){
s += abs(ret[i]);
}
if (s <= mn){
mn = s;
ans = x;
}
}
}
cout << "YES\n";
solve(ans);
for (int i = 1; i<=n; i++) cout << ret[i] << " ";
cout << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Sum of endpoints for edge (1; 3) differs from the expected value 2. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Sum of endpoints for edge (1; 3) differs from the expected value 2. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Sum of endpoints for edge (1; 3) differs from the expected value 2. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Sum of endpoints for edge (1; 3) differs from the expected value 2. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Sum of endpoints for edge (1; 3) differs from the expected value 2. |
2 |
Halted |
0 ms |
0 KB |
- |