이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
const int MN = 2e5+5;
//start at 1:43
int E[MN],dep[MN],cnt[MN],par[MN],odd[MN],cur;
double C[MN],sum[MN],val[MN];
vector<int> G[MN],group;
vector<double> tmp;
bool vis[MN],fail;
void dfs(int s)
{
vis[s] = 1;
for(int d:G[s]){
if(odd[cur]) break;
int e = E[d]-s;
if(vis[e]){
if(dep[e]<dep[s]) continue;
if((dep[e]-dep[s])%2){
if( (cnt[e]-cnt[par[s]]) - (cnt[par[e]]-cnt[s]) != C[d]-1) fail = 1;
}
else{
odd[cur] = s;
val[s] = (-sum[e]+sum[s]+sum[par[e]]-sum[par[s]]+C[d])/2.0;
}
}
else{
par[e] = s;
dep[e] = dep[s]+1;
cnt[e] = cnt[par[s]];
if(C[d]==2) cnt[e]++;
sum[e] = sum[par[s]]+C[d];
dfs(e);
}
}
}
void solve(int s)
{
group.push_back(s);
vis[s] = 1;
for(int d:G[s]){
int e = E[d]-s;
if(vis[e]){
if(odd&&val[s]+val[e]!=C[d]) fail = 1;
}
else{
val[e] = C[d]-val[s];
tmp.push_back(dep[e]%2 ? -val[e] : val[e]);
solve(e);
}
}
}
int N,M;
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0);
cin >> N >> M;
for(int i=0; i<M; i++){
int a,b;
cin >> a >> b >> C[i];
E[i] = a+b;
G[a].push_back(i);
G[b].push_back(i);
}
for(int i=1; i<=N; i++){
if(vis[i]) continue;
cur = i;
dfs(i);
if(!odd[i]) odd[i] = -1;
}
fill(vis,vis+N+1,0);
for(int i=1; i<=N; i++){
if(!odd[i]) continue;
if(odd[i]>0) solve(odd[i]);
else{
group.clear();
tmp.clear();
solve(i);
sort(all(group));
double x = -tmp[(group.size()+1)/2];
for(int s:group){
if(dep[s]%2) val[s] -= x;
else val[s] += x;
}
}
}
fixed(cout);
cout.precision(6);
if(fail) cout << "NO";
else{
cout << "YES\n";
for(int i=1; i<=N; i++) cout << val[i] << ' ';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |