답안 #747187

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
747187 2023-05-23T20:53:41 Z Sam_a17 Graph (BOI20_graph) C++17
0 / 100
4 ms 5048 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
 
#define pb push_back
#define popf pop_front
#define popb pop_back
 
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
 
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T> void print(T v[],T n) {cerr << "["; for(int i = 0; i < n; i++) {cerr << v[i] << " ";} cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
 
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
 
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();
 
  if (str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  }
}
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 10;
vector<pair<int, int>> adj[N];
bool vis[N];
int n, m;

map<pair<int, int>, int> edge;

int color[N];
int p[N], to[N];
vector<int> cycle;

double ans[N];

vector<int> component;

void dfs(int node, int parent) {
  p[node] = parent;
  component.push_back(node);
  vis[node] = true;
  for(auto i: adj[node]) {
    if(i.first == parent) continue;
    to[i.first] = i.second;
    if(color[i.first] == color[node]) {
      // get odd cycle
      if(!sz(cycle)) {
        int curr = node;
        while(curr) {
          cycle.push_back(curr);
          curr = p[curr];
        }
      }
    } else if(!color[i.first]) {
      color[i.first] = 3 - color[node];
      dfs(i.first, node);
    }
  }
}


int ok = 1;
bool can[N];

void dfsCheck(int node, int parent) {
  // dbg(make_pair(node, parent))
  // dbg(node)
  // dbg(adj[node])
  for(auto i: adj[node]) {
    if(i.first == parent) continue;
    double cost = i.second;

    if(can[i.first] && cost - ans[node] != ans[i.first]) {
      ok = 0;
    } else if(!can[i.first]) {
      can[i.first] = true;
      ans[i.first] = cost - ans[node];
      dfsCheck(i.first, node);
    }
  }
}

double add[N];
int lst[N], par[N];

vector<int> b;

void dfs2(int node, int parent) {
  par[node] = (par[parent] + 1) % 2;
  vis[node] = true;
  // dbg(make_pair(node, parent))
  if(par[node]) {
    b.push_back(lst[node]);  
  } else {
    b.push_back(-lst[node]);  
  }

  for(auto i: adj[node]) {
    if(i.first == parent) continue;
    if(vis[i.first]) continue;
    lst[i.first] = i.second - lst[node];
    dfs2(i.first, node);
  }
}

void solve_() {
  cin >> n >> m;

  int gok = 1;
  for(int i = 1; i <= m; i++) {
    int a, b, c; cin >> a >> b >> c;
    if(a < b) {
      swap(a, b);
    }

    adj[a].emplace_back(b, c);
    adj[b].emplace_back(a, c);

    if(edge.find({a, b}) != edge.end() && edge[{a, b}] != c) {
      gok = 0;
    }

    edge[{a, b}] = c;
  }

  if(!gok) {
    cout << "NO" << endl;
    return;
  }

  for(int i = 1; i <= n; i++) {
    if(!vis[i]) {
      color[i] = 1;
      cycle.clear();
      component.clear();

      dfs(i, 0);

      if(sz(cycle)) {
        double s = 0;
        reverse(all(cycle));

        for(int i = 0; i < sz(cycle); i++) {
          int j = (i + 1) % sz(cycle);
          int a = cycle[i], b = cycle[j];

          if(a < b) {
            swap(a, b);
          }

          if(i % 2 == 0) {
            s += edge[{a, b}];
          } else {
            s -= edge[{a, b}];
          }
        }
        s /= 2;

        //
        ans[i] = s;
        can[i] = true;

        ok = 1;
        dfsCheck(i, 0);

        if(!ok) {
          cout << "NO" << endl;
          return;
        }
      } else {
        b.clear();
        // dbg(component)
        for(auto j: component) {
          vis[j] = false;
        }
        vis[i] = true;
        b.push_back(0);
        for(auto j: adj[i]) {
          if(vis[j.first]) continue;
          lst[j.first] = j.second;
          dfs2(j.first, i);
        }

        sort(all(b));
        // dbg(b)
        
        int med = sz(b) / 2;
        
        if(sz(b) & 1) {
          ans[i] = b[med];
          can[i] = true;
        } else {
          ans[i] = b[med] - b[med - 1];
          ans[i] /= 2;
          can[i] = true;
        }
        
        // dbg(make_pair(ans[i], can[i]))
        // dbg(can[2])
        ok = 1;
        dfsCheck(i, 0);
        if(!ok) {
          cout << "NO" << endl;
          return;
        }
      }
    }
  }

  cout << "YES" << endl;
  for(int i = 1; i <= n; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
}
 
int main() {
  setIO();
  
  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };
 
  int test_cases = 1; 
  // cin >> test_cases;
  solve(test_cases);
 
  return 0;
}

Compilation message

Graph.cpp: In function 'void setIO(std::string)':
Graph.cpp:62:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Graph.cpp:63:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB answer = YES
2 Correct 4 ms 4992 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 4948 KB answer = NO
5 Correct 3 ms 4948 KB answer = YES
6 Correct 3 ms 4948 KB answer = YES
7 Correct 3 ms 5048 KB answer = YES
8 Incorrect 3 ms 5044 KB participant answer is larger than the answer of jury
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB answer = YES
2 Correct 4 ms 4992 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 4948 KB answer = NO
5 Correct 3 ms 4948 KB answer = YES
6 Correct 3 ms 4948 KB answer = YES
7 Correct 3 ms 5048 KB answer = YES
8 Incorrect 3 ms 5044 KB participant answer is larger than the answer of jury
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB answer = YES
2 Correct 4 ms 4992 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 4948 KB answer = NO
5 Correct 3 ms 4948 KB answer = YES
6 Correct 3 ms 4948 KB answer = YES
7 Correct 3 ms 5048 KB answer = YES
8 Incorrect 3 ms 5044 KB participant answer is larger than the answer of jury
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB answer = YES
2 Correct 4 ms 4992 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 4948 KB answer = NO
5 Correct 3 ms 4948 KB answer = YES
6 Correct 3 ms 4948 KB answer = YES
7 Correct 3 ms 5048 KB answer = YES
8 Incorrect 3 ms 5044 KB participant answer is larger than the answer of jury
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB answer = YES
2 Correct 4 ms 4992 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 4948 KB answer = NO
5 Correct 3 ms 4948 KB answer = YES
6 Correct 3 ms 4948 KB answer = YES
7 Correct 3 ms 5048 KB answer = YES
8 Incorrect 3 ms 5044 KB participant answer is larger than the answer of jury
9 Halted 0 ms 0 KB -