# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
806308 |
2023-08-04T05:52:10 Z |
pawned |
Graph (BOI20_graph) |
C++17 |
|
2 ms |
2656 KB |
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
int N, M;
vector<pair<ii, ll>> edges;
vector<pair<int, ll>> adj[100005];
ll parity[100005]; // +x or -x
ll base[100005]; // [k] +- x
bool vis[100005];
vi comp;
void dfs(int n) {
vis[n] = true;
comp.pb(n);
for (pair<int, ll> i : adj[n]) {
if (!vis[i.fi]) {
parity[i.fi] = -parity[n];
base[i.fi] = i.se - base[n];
dfs(i.fi);
}
}
}
bool solve() {
cin>>N>>M;
for (int i = 0; i < M; i++) {
int u, v;
cin>>u>>v;
u--; v--;
ll w;
cin>>w;
w *= 2; // multiply by 2 to avoid .5 decimals
edges.pb({{u, v}, w});
adj[u].pb({v, w});
adj[v].pb({u, w});
}
ll total = 0;
ll ans[N] = {0};
for (int i = 0; i < M; i++) {
if (vis[i])
continue;
comp.clear();
parity[i] = 1;
base[i] = 0;
dfs(i);
/*
cout<<"comp: ";
for (int j : comp) {
cout<<"{"<<j<<", "<<parity[j]<<", "<<base[j]<<"}; ";
}
cout<<endl;
*/
ll xval = 1e9;
for (int j : comp) {
// cout<<"at "<<j<<endl;
for (pair<int, ll> k : adj[j]) {
ll pari1 = parity[j];
ll base1 = base[j];
ll pari2 = parity[k.fi];
ll base2 = base[k.fi];
if (pari1 != pari2) {
if (base1 + base2 != k.se)
return false;
}
else {
// cout<<"tight: "<<j<<" "<<k.fi<<endl;
ll parisum = pari1 + pari2; // either -2x or 2x
ll basesum = base1 + base2;
// goal is to sum to k.se
ll res = (k.se - basesum) / parisum;
// cout<<"needed res: "<<res<<endl;
if (xval == 1e9)
xval = res;
else {
if (xval != res)
return false;
}
}
}
}
// cout<<"xval: "<<xval<<endl;
if (xval != 1e9) {
ll sum = 0;
for (int j : comp) {
ans[j] = xval * parity[j] + base[j];
sum += (xval * parity[j] + base[j]);
}
total += sum;
}
else {
// any median will do
ll K = comp.size();
ll all[K];
for (int j = 0; j < K; j++) {
all[j] = parity[j] * base[j];
}
/*
for (int j : all)
cout<<"yay "<<j<<endl;
*/
sort(all, all + K);
ll median = -all[K / 2]; // make this zero
// cout<<"median is "<<median<<endl;
ll sum = 0;
for (int j : comp) {
sum += abs(parity[j] * median + base[j]);
ans[j] = parity[j] * median + base[j];
}
total += sum;
}
// made a tree, now check all edges are satisfied
}
// cout<<"total: "<<total<<endl;
cout<<"YES"<<endl;
for (int i = 0; i < N; i++) {
printf("%.10f ", ans[i] / 2.0);
}
cout<<endl;
return true;
}
int main() {
if (!solve())
cout<<"NO"<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
answer = YES |
2 |
Correct |
1 ms |
2644 KB |
answer = YES |
3 |
Correct |
1 ms |
2656 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Incorrect |
1 ms |
2644 KB |
Sum of endpoints for edge (5; 3) differs from the expected value 2. |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
answer = YES |
2 |
Correct |
1 ms |
2644 KB |
answer = YES |
3 |
Correct |
1 ms |
2656 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Incorrect |
1 ms |
2644 KB |
Sum of endpoints for edge (5; 3) differs from the expected value 2. |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
answer = YES |
2 |
Correct |
1 ms |
2644 KB |
answer = YES |
3 |
Correct |
1 ms |
2656 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Incorrect |
1 ms |
2644 KB |
Sum of endpoints for edge (5; 3) differs from the expected value 2. |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
answer = YES |
2 |
Correct |
1 ms |
2644 KB |
answer = YES |
3 |
Correct |
1 ms |
2656 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Incorrect |
1 ms |
2644 KB |
Sum of endpoints for edge (5; 3) differs from the expected value 2. |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
answer = YES |
2 |
Correct |
1 ms |
2644 KB |
answer = YES |
3 |
Correct |
1 ms |
2656 KB |
answer = YES |
4 |
Correct |
2 ms |
2644 KB |
answer = NO |
5 |
Incorrect |
1 ms |
2644 KB |
Sum of endpoints for edge (5; 3) differs from the expected value 2. |
6 |
Halted |
0 ms |
0 KB |
- |