답안 #747467

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
747467 2023-05-24T08:07:51 Z Sam_a17 Graph (BOI20_graph) C++17
100 / 100
368 ms 43488 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 != i.first) {
          cycle.push_back(curr);
          curr = p[curr];
        }
        cycle.push_back(i.first);
      }
    } 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) {
  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;
  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));
        // dbg(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[cycle.front()] = s;
        can[cycle.front()] = true;

        ok = 1;
        dfsCheck(cycle.front(), 0);

        if(!ok) {
          cout << "NO" << endl;
          return;
        }
      } else {
        // dbg("her")
        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));
        // uniq(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;
        }
        
        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 5036 KB answer = YES
2 Correct 3 ms 4948 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 4948 KB answer = NO
5 Correct 3 ms 5044 KB answer = YES
6 Correct 3 ms 5040 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 3 ms 4948 KB answer = NO
10 Correct 3 ms 4948 KB answer = YES
11 Correct 4 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 3 ms 5044 KB answer = YES
14 Correct 3 ms 4948 KB answer = YES
15 Correct 3 ms 4948 KB answer = YES
16 Correct 3 ms 4948 KB answer = YES
17 Correct 3 ms 4948 KB answer = YES
18 Correct 3 ms 4948 KB answer = YES
19 Correct 3 ms 4948 KB answer = YES
20 Correct 3 ms 4948 KB answer = YES
21 Correct 3 ms 4948 KB answer = YES
22 Correct 4 ms 4948 KB answer = NO
23 Correct 3 ms 5048 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5036 KB answer = YES
2 Correct 3 ms 4948 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 4948 KB answer = NO
5 Correct 3 ms 5044 KB answer = YES
6 Correct 3 ms 5040 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 3 ms 4948 KB answer = NO
10 Correct 3 ms 4948 KB answer = YES
11 Correct 4 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 3 ms 5044 KB answer = YES
14 Correct 3 ms 4948 KB answer = YES
15 Correct 3 ms 4948 KB answer = YES
16 Correct 3 ms 4948 KB answer = YES
17 Correct 3 ms 4948 KB answer = YES
18 Correct 3 ms 4948 KB answer = YES
19 Correct 3 ms 4948 KB answer = YES
20 Correct 3 ms 4948 KB answer = YES
21 Correct 3 ms 4948 KB answer = YES
22 Correct 4 ms 4948 KB answer = NO
23 Correct 3 ms 5048 KB answer = NO
24 Correct 3 ms 5040 KB answer = YES
25 Correct 3 ms 4948 KB answer = YES
26 Correct 3 ms 5076 KB answer = YES
27 Correct 3 ms 5044 KB answer = YES
28 Correct 3 ms 4948 KB answer = YES
29 Correct 3 ms 4948 KB answer = YES
30 Correct 3 ms 5076 KB answer = NO
31 Correct 3 ms 5048 KB answer = YES
32 Correct 3 ms 4948 KB answer = YES
33 Correct 3 ms 4948 KB answer = YES
34 Correct 3 ms 4948 KB answer = YES
35 Correct 3 ms 5040 KB answer = YES
36 Correct 4 ms 5048 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5036 KB answer = YES
2 Correct 3 ms 4948 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 4948 KB answer = NO
5 Correct 3 ms 5044 KB answer = YES
6 Correct 3 ms 5040 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 3 ms 4948 KB answer = NO
10 Correct 3 ms 4948 KB answer = YES
11 Correct 4 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 3 ms 5044 KB answer = YES
14 Correct 3 ms 4948 KB answer = YES
15 Correct 3 ms 4948 KB answer = YES
16 Correct 3 ms 4948 KB answer = YES
17 Correct 3 ms 4948 KB answer = YES
18 Correct 3 ms 4948 KB answer = YES
19 Correct 3 ms 4948 KB answer = YES
20 Correct 3 ms 4948 KB answer = YES
21 Correct 3 ms 4948 KB answer = YES
22 Correct 4 ms 4948 KB answer = NO
23 Correct 3 ms 5048 KB answer = NO
24 Correct 3 ms 5040 KB answer = YES
25 Correct 3 ms 4948 KB answer = YES
26 Correct 3 ms 5076 KB answer = YES
27 Correct 3 ms 5044 KB answer = YES
28 Correct 3 ms 4948 KB answer = YES
29 Correct 3 ms 4948 KB answer = YES
30 Correct 3 ms 5076 KB answer = NO
31 Correct 3 ms 5048 KB answer = YES
32 Correct 3 ms 4948 KB answer = YES
33 Correct 3 ms 4948 KB answer = YES
34 Correct 3 ms 4948 KB answer = YES
35 Correct 3 ms 5040 KB answer = YES
36 Correct 4 ms 5048 KB answer = YES
37 Correct 3 ms 5076 KB answer = YES
38 Correct 3 ms 5076 KB answer = YES
39 Correct 3 ms 5076 KB answer = YES
40 Correct 4 ms 5180 KB answer = YES
41 Correct 3 ms 5204 KB answer = NO
42 Correct 4 ms 5204 KB answer = YES
43 Correct 4 ms 5140 KB answer = YES
44 Correct 4 ms 5204 KB answer = YES
45 Correct 4 ms 5076 KB answer = YES
46 Correct 4 ms 5076 KB answer = YES
47 Correct 4 ms 5204 KB answer = YES
48 Correct 4 ms 5204 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5036 KB answer = YES
2 Correct 3 ms 4948 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 4948 KB answer = NO
5 Correct 3 ms 5044 KB answer = YES
6 Correct 3 ms 5040 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 3 ms 4948 KB answer = NO
10 Correct 3 ms 4948 KB answer = YES
11 Correct 4 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 3 ms 5044 KB answer = YES
14 Correct 3 ms 4948 KB answer = YES
15 Correct 3 ms 4948 KB answer = YES
16 Correct 3 ms 4948 KB answer = YES
17 Correct 3 ms 4948 KB answer = YES
18 Correct 3 ms 4948 KB answer = YES
19 Correct 3 ms 4948 KB answer = YES
20 Correct 3 ms 4948 KB answer = YES
21 Correct 3 ms 4948 KB answer = YES
22 Correct 4 ms 4948 KB answer = NO
23 Correct 3 ms 5048 KB answer = NO
24 Correct 3 ms 5040 KB answer = YES
25 Correct 3 ms 4948 KB answer = YES
26 Correct 3 ms 5076 KB answer = YES
27 Correct 3 ms 5044 KB answer = YES
28 Correct 3 ms 4948 KB answer = YES
29 Correct 3 ms 4948 KB answer = YES
30 Correct 3 ms 5076 KB answer = NO
31 Correct 3 ms 5048 KB answer = YES
32 Correct 3 ms 4948 KB answer = YES
33 Correct 3 ms 4948 KB answer = YES
34 Correct 3 ms 4948 KB answer = YES
35 Correct 3 ms 5040 KB answer = YES
36 Correct 4 ms 5048 KB answer = YES
37 Correct 3 ms 5076 KB answer = YES
38 Correct 3 ms 5076 KB answer = YES
39 Correct 3 ms 5076 KB answer = YES
40 Correct 4 ms 5180 KB answer = YES
41 Correct 3 ms 5204 KB answer = NO
42 Correct 4 ms 5204 KB answer = YES
43 Correct 4 ms 5140 KB answer = YES
44 Correct 4 ms 5204 KB answer = YES
45 Correct 4 ms 5076 KB answer = YES
46 Correct 4 ms 5076 KB answer = YES
47 Correct 4 ms 5204 KB answer = YES
48 Correct 4 ms 5204 KB answer = YES
49 Correct 17 ms 6592 KB answer = YES
50 Correct 16 ms 7064 KB answer = YES
51 Correct 16 ms 7192 KB answer = YES
52 Correct 10 ms 7136 KB answer = NO
53 Correct 4 ms 5204 KB answer = YES
54 Correct 5 ms 5404 KB answer = YES
55 Correct 9 ms 5844 KB answer = YES
56 Correct 14 ms 6592 KB answer = YES
57 Correct 16 ms 6484 KB answer = YES
58 Correct 12 ms 6356 KB answer = YES
59 Correct 12 ms 6228 KB answer = YES
60 Correct 14 ms 6484 KB answer = YES
61 Correct 9 ms 5684 KB answer = YES
62 Correct 218 ms 24340 KB answer = NO
63 Correct 238 ms 25568 KB answer = YES
64 Correct 231 ms 25420 KB answer = NO
65 Correct 228 ms 25484 KB answer = YES
66 Correct 5 ms 5332 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5036 KB answer = YES
2 Correct 3 ms 4948 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 4948 KB answer = NO
5 Correct 3 ms 5044 KB answer = YES
6 Correct 3 ms 5040 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 3 ms 4948 KB answer = NO
10 Correct 3 ms 4948 KB answer = YES
11 Correct 4 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 3 ms 5044 KB answer = YES
14 Correct 3 ms 4948 KB answer = YES
15 Correct 3 ms 4948 KB answer = YES
16 Correct 3 ms 4948 KB answer = YES
17 Correct 3 ms 4948 KB answer = YES
18 Correct 3 ms 4948 KB answer = YES
19 Correct 3 ms 4948 KB answer = YES
20 Correct 3 ms 4948 KB answer = YES
21 Correct 3 ms 4948 KB answer = YES
22 Correct 4 ms 4948 KB answer = NO
23 Correct 3 ms 5048 KB answer = NO
24 Correct 3 ms 5040 KB answer = YES
25 Correct 3 ms 4948 KB answer = YES
26 Correct 3 ms 5076 KB answer = YES
27 Correct 3 ms 5044 KB answer = YES
28 Correct 3 ms 4948 KB answer = YES
29 Correct 3 ms 4948 KB answer = YES
30 Correct 3 ms 5076 KB answer = NO
31 Correct 3 ms 5048 KB answer = YES
32 Correct 3 ms 4948 KB answer = YES
33 Correct 3 ms 4948 KB answer = YES
34 Correct 3 ms 4948 KB answer = YES
35 Correct 3 ms 5040 KB answer = YES
36 Correct 4 ms 5048 KB answer = YES
37 Correct 3 ms 5076 KB answer = YES
38 Correct 3 ms 5076 KB answer = YES
39 Correct 3 ms 5076 KB answer = YES
40 Correct 4 ms 5180 KB answer = YES
41 Correct 3 ms 5204 KB answer = NO
42 Correct 4 ms 5204 KB answer = YES
43 Correct 4 ms 5140 KB answer = YES
44 Correct 4 ms 5204 KB answer = YES
45 Correct 4 ms 5076 KB answer = YES
46 Correct 4 ms 5076 KB answer = YES
47 Correct 4 ms 5204 KB answer = YES
48 Correct 4 ms 5204 KB answer = YES
49 Correct 17 ms 6592 KB answer = YES
50 Correct 16 ms 7064 KB answer = YES
51 Correct 16 ms 7192 KB answer = YES
52 Correct 10 ms 7136 KB answer = NO
53 Correct 4 ms 5204 KB answer = YES
54 Correct 5 ms 5404 KB answer = YES
55 Correct 9 ms 5844 KB answer = YES
56 Correct 14 ms 6592 KB answer = YES
57 Correct 16 ms 6484 KB answer = YES
58 Correct 12 ms 6356 KB answer = YES
59 Correct 12 ms 6228 KB answer = YES
60 Correct 14 ms 6484 KB answer = YES
61 Correct 9 ms 5684 KB answer = YES
62 Correct 218 ms 24340 KB answer = NO
63 Correct 238 ms 25568 KB answer = YES
64 Correct 231 ms 25420 KB answer = NO
65 Correct 228 ms 25484 KB answer = YES
66 Correct 5 ms 5332 KB answer = YES
67 Correct 121 ms 31028 KB answer = YES
68 Correct 113 ms 30900 KB answer = YES
69 Correct 133 ms 29988 KB answer = YES
70 Correct 170 ms 43488 KB answer = YES
71 Correct 131 ms 30188 KB answer = YES
72 Correct 194 ms 20392 KB answer = YES
73 Correct 197 ms 19204 KB answer = YES
74 Correct 106 ms 19620 KB answer = YES
75 Correct 79 ms 20216 KB answer = NO
76 Correct 19 ms 6868 KB answer = YES
77 Correct 33 ms 8912 KB answer = YES
78 Correct 68 ms 11668 KB answer = YES
79 Correct 168 ms 18104 KB answer = YES
80 Correct 121 ms 20468 KB answer = YES
81 Correct 130 ms 24304 KB answer = NO
82 Correct 193 ms 29212 KB answer = YES
83 Correct 221 ms 30096 KB answer = YES
84 Correct 205 ms 30948 KB answer = YES
85 Correct 117 ms 30860 KB answer = YES
86 Correct 129 ms 30016 KB answer = YES
87 Correct 155 ms 22176 KB answer = NO
88 Correct 229 ms 24260 KB answer = YES
89 Correct 205 ms 18204 KB answer = YES
90 Correct 240 ms 18252 KB answer = YES
91 Correct 193 ms 18348 KB answer = YES
92 Correct 76 ms 12240 KB answer = YES
93 Correct 86 ms 12216 KB answer = YES
94 Correct 153 ms 30512 KB answer = NO
95 Correct 105 ms 17936 KB answer = NO
96 Correct 368 ms 36456 KB answer = YES
97 Correct 86 ms 30568 KB answer = NO