답안 #1030652

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1030652 2024-07-22T08:10:49 Z ttamx Graph (BOI20_graph) C++17
0 / 100
4 ms 5464 KB
#include<bits/stdc++.h>

using namespace std;

using db = double;

const int N=1e5+5;

int n,m;
vector<pair<int,int>> adj[N];
bool vis[N];
int a[N],b[N],ans[N];
vector<int> comp;
pair<int,int> val;
bool found;

int dfs(int u){
    comp.emplace_back(u);
    vis[u]=true;
    for(auto [v,w]:adj[u]){
        if(!vis[v]){
            a[v]=-a[u];
            b[v]=w-b[u];
            dfs(v);
        }else{
            if(a[u]+a[v]==0){
                if(b[u]+b[v]!=w){
                    cout << "NO",exit(0);
                }
            }else{
                int num=w-b[u]-b[v];
                int den=a[u]+a[v];
                if(!found){
                    found=true;
                    val={num,den};
                }else if(num*val.second!=val.first*den){
                    cout << "NO",exit(0);
                }
            }
        }
    }
}

inline int calc(int x){
    int res=0;
    for(auto u:comp){
        res+=abs(a[u]*x+b[u]*2);
    }
    return res;
}

inline void answer(int x){
    for(auto u:comp){
        ans[u]=a[u]*x+b[u]*2;
    }
}

int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n >> m;
    for(int i=1;i<=m;i++){
        int u,v,w;
        cin >> u >> v >> w;
        adj[u].emplace_back(v,w);
        adj[v].emplace_back(u,w);
    }
    for(int i=1;i<=n;i++)if(!vis[i]){
        comp.clear();
        found=false;
        a[i]=1,b[i]=0;
        dfs(i);
        if(found){
            answer(val.first*2/val.second);
        }else{
            int c=comp.size();
            int l=-c*2-1,r=c*2+1;
            while(l<r){
                int m=l+(r-l)/2;
                if(calc(m)<calc(m+1))r=m;
                else l=m+1;
            }
            answer(l);
        }
    }
    cout << "YES\n";
    for(int i=1;i<=n;i++)cout << db(ans[i])/2.0 << " ";
}

Compilation message

Graph.cpp: In function 'int dfs(int)':
Graph.cpp:42:1: warning: control reaches end of non-void function [-Wreturn-type]
   42 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5464 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5464 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5464 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5464 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5464 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -