답안 #932220

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
932220 2024-02-23T05:43:29 Z UmairAhmadMirza Graph (BOI20_graph) C++17
0 / 100
1 ms 604 KB
#include <bits/stdc++.h>
using namespace std;
int const N=1e3+5;
double const inf=1e9+5;
vector<pair<int,int>> adj[N];
int edge[N][N];
vector<int> com[N];
int cc=0;
int rep[N];
double X[N];
double ct[N];
double val[N];
int n,m;
int cc_find(int node,int xx,int c){
  ct[node]=c;
  X[node]=xx;
  rep[node]=cc;
  com[cc].push_back(node);
  for(auto i:adj[node])
    if(rep[i.first]==0)
      cc_find(i.first,-xx,i.second-c);
}
double answer(int c,double x){
  double v=0.0;
  for(int i:com[c])
    for(int j:com[c])
      if(edge[i][j]>0){
        v=(X[i]+X[j])*x;
        v+=ct[i]+ct[j];
        if(v!=edge[i][j])
          return inf;
      }
  double sm=0.0;
  for(auto i:com[c])
    sm+=abs((X[i]*x)+ct[i]);
  return sm;
}
int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    int u,v,w;
    cin>>u>>v>>w;
    adj[u].push_back({v,w});
    adj[v].push_back({u,w});
    if(edge[u][v]>0 && edge[u][v]!=w){
      cout<<"NO"<<endl;
      return 0;
    }
    edge[v][u]=w;
    edge[u][v]=w;
  }
  for(int i=1;i<=n;i++){
    if(rep[i]==0){
      cc++;
      cc_find(i,1,0);
    }
  }
  double ans=0.0;
  for(int c=1;c<=cc;c++){
    double sm=inf;
    double xx=-3;
    for(double x=-2.0;x<=2.0;x+=0.5){
      if(answer(c,x)<sm){
        sm=answer(c,x);
        xx=x;
      }
    }
    if(sm==inf){
      cout<<"NO"<<endl;
      return 0;
    }
    for(auto i:com[c])
      val[i]=(X[i]*xx)+ct[i];
  }
  cout<<fixed;
  cout<<"YES"<<endl;
  for(int i=1;i<=n;i++)
    cout<<setprecision(10)<<val[i]<<' ';
  cout<<endl;
  // cout<<ans<<endl;
}

Compilation message

Graph.cpp: In function 'int cc_find(int, int, int)':
Graph.cpp:22:1: warning: no return statement in function returning non-void [-Wreturn-type]
   22 | }
      | ^
Graph.cpp: In function 'int main()':
Graph.cpp:58:10: warning: unused variable 'ans' [-Wunused-variable]
   58 |   double ans=0.0;
      |          ^~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -