답안 #747194

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
747194 2023-05-23T21:35:03 Z Sam_a17 Graph (BOI20_graph) C++17
100 / 100
566 ms 40788 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) {
  // 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;
  }

  dbg("her")
  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 4948 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 2 ms 4948 KB answer = YES
6 Correct 2 ms 4948 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 2 ms 4948 KB answer = NO
10 Correct 3 ms 4948 KB answer = YES
11 Correct 3 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 4 ms 4948 KB answer = YES
14 Correct 4 ms 4948 KB answer = YES
15 Correct 4 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 3 ms 4948 KB answer = NO
23 Correct 2 ms 4948 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 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 2 ms 4948 KB answer = YES
6 Correct 2 ms 4948 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 2 ms 4948 KB answer = NO
10 Correct 3 ms 4948 KB answer = YES
11 Correct 3 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 4 ms 4948 KB answer = YES
14 Correct 4 ms 4948 KB answer = YES
15 Correct 4 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 3 ms 4948 KB answer = NO
23 Correct 2 ms 4948 KB answer = NO
24 Correct 3 ms 4948 KB answer = YES
25 Correct 3 ms 4948 KB answer = YES
26 Correct 3 ms 5076 KB answer = YES
27 Correct 3 ms 4948 KB answer = YES
28 Correct 3 ms 4948 KB answer = YES
29 Correct 3 ms 4948 KB answer = YES
30 Correct 3 ms 4948 KB answer = NO
31 Correct 3 ms 4948 KB answer = YES
32 Correct 3 ms 4948 KB answer = YES
33 Correct 3 ms 4948 KB answer = YES
34 Correct 3 ms 5076 KB answer = YES
35 Correct 3 ms 4948 KB answer = YES
36 Correct 3 ms 4948 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 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 2 ms 4948 KB answer = YES
6 Correct 2 ms 4948 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 2 ms 4948 KB answer = NO
10 Correct 3 ms 4948 KB answer = YES
11 Correct 3 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 4 ms 4948 KB answer = YES
14 Correct 4 ms 4948 KB answer = YES
15 Correct 4 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 3 ms 4948 KB answer = NO
23 Correct 2 ms 4948 KB answer = NO
24 Correct 3 ms 4948 KB answer = YES
25 Correct 3 ms 4948 KB answer = YES
26 Correct 3 ms 5076 KB answer = YES
27 Correct 3 ms 4948 KB answer = YES
28 Correct 3 ms 4948 KB answer = YES
29 Correct 3 ms 4948 KB answer = YES
30 Correct 3 ms 4948 KB answer = NO
31 Correct 3 ms 4948 KB answer = YES
32 Correct 3 ms 4948 KB answer = YES
33 Correct 3 ms 4948 KB answer = YES
34 Correct 3 ms 5076 KB answer = YES
35 Correct 3 ms 4948 KB answer = YES
36 Correct 3 ms 4948 KB answer = YES
37 Correct 3 ms 5076 KB answer = YES
38 Correct 3 ms 5076 KB answer = YES
39 Correct 4 ms 5076 KB answer = YES
40 Correct 3 ms 5204 KB answer = YES
41 Correct 3 ms 5204 KB answer = NO
42 Correct 4 ms 5204 KB answer = YES
43 Correct 4 ms 5204 KB answer = YES
44 Correct 4 ms 5204 KB answer = YES
45 Correct 4 ms 5076 KB answer = YES
46 Correct 3 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 4948 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 2 ms 4948 KB answer = YES
6 Correct 2 ms 4948 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 2 ms 4948 KB answer = NO
10 Correct 3 ms 4948 KB answer = YES
11 Correct 3 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 4 ms 4948 KB answer = YES
14 Correct 4 ms 4948 KB answer = YES
15 Correct 4 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 3 ms 4948 KB answer = NO
23 Correct 2 ms 4948 KB answer = NO
24 Correct 3 ms 4948 KB answer = YES
25 Correct 3 ms 4948 KB answer = YES
26 Correct 3 ms 5076 KB answer = YES
27 Correct 3 ms 4948 KB answer = YES
28 Correct 3 ms 4948 KB answer = YES
29 Correct 3 ms 4948 KB answer = YES
30 Correct 3 ms 4948 KB answer = NO
31 Correct 3 ms 4948 KB answer = YES
32 Correct 3 ms 4948 KB answer = YES
33 Correct 3 ms 4948 KB answer = YES
34 Correct 3 ms 5076 KB answer = YES
35 Correct 3 ms 4948 KB answer = YES
36 Correct 3 ms 4948 KB answer = YES
37 Correct 3 ms 5076 KB answer = YES
38 Correct 3 ms 5076 KB answer = YES
39 Correct 4 ms 5076 KB answer = YES
40 Correct 3 ms 5204 KB answer = YES
41 Correct 3 ms 5204 KB answer = NO
42 Correct 4 ms 5204 KB answer = YES
43 Correct 4 ms 5204 KB answer = YES
44 Correct 4 ms 5204 KB answer = YES
45 Correct 4 ms 5076 KB answer = YES
46 Correct 3 ms 5076 KB answer = YES
47 Correct 4 ms 5204 KB answer = YES
48 Correct 4 ms 5204 KB answer = YES
49 Correct 13 ms 6484 KB answer = YES
50 Correct 15 ms 6868 KB answer = YES
51 Correct 15 ms 7072 KB answer = YES
52 Correct 13 ms 6996 KB answer = NO
53 Correct 4 ms 5204 KB answer = YES
54 Correct 5 ms 5332 KB answer = YES
55 Correct 10 ms 5784 KB answer = YES
56 Correct 13 ms 6500 KB answer = YES
57 Correct 13 ms 6324 KB answer = YES
58 Correct 12 ms 6220 KB answer = YES
59 Correct 12 ms 6228 KB answer = YES
60 Correct 14 ms 6464 KB answer = YES
61 Correct 8 ms 5716 KB answer = YES
62 Correct 191 ms 22128 KB answer = NO
63 Correct 232 ms 23116 KB answer = YES
64 Correct 228 ms 23116 KB answer = NO
65 Correct 223 ms 23124 KB answer = YES
66 Correct 4 ms 5332 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 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 2 ms 4948 KB answer = YES
6 Correct 2 ms 4948 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 2 ms 4948 KB answer = NO
10 Correct 3 ms 4948 KB answer = YES
11 Correct 3 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 4 ms 4948 KB answer = YES
14 Correct 4 ms 4948 KB answer = YES
15 Correct 4 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 3 ms 4948 KB answer = NO
23 Correct 2 ms 4948 KB answer = NO
24 Correct 3 ms 4948 KB answer = YES
25 Correct 3 ms 4948 KB answer = YES
26 Correct 3 ms 5076 KB answer = YES
27 Correct 3 ms 4948 KB answer = YES
28 Correct 3 ms 4948 KB answer = YES
29 Correct 3 ms 4948 KB answer = YES
30 Correct 3 ms 4948 KB answer = NO
31 Correct 3 ms 4948 KB answer = YES
32 Correct 3 ms 4948 KB answer = YES
33 Correct 3 ms 4948 KB answer = YES
34 Correct 3 ms 5076 KB answer = YES
35 Correct 3 ms 4948 KB answer = YES
36 Correct 3 ms 4948 KB answer = YES
37 Correct 3 ms 5076 KB answer = YES
38 Correct 3 ms 5076 KB answer = YES
39 Correct 4 ms 5076 KB answer = YES
40 Correct 3 ms 5204 KB answer = YES
41 Correct 3 ms 5204 KB answer = NO
42 Correct 4 ms 5204 KB answer = YES
43 Correct 4 ms 5204 KB answer = YES
44 Correct 4 ms 5204 KB answer = YES
45 Correct 4 ms 5076 KB answer = YES
46 Correct 3 ms 5076 KB answer = YES
47 Correct 4 ms 5204 KB answer = YES
48 Correct 4 ms 5204 KB answer = YES
49 Correct 13 ms 6484 KB answer = YES
50 Correct 15 ms 6868 KB answer = YES
51 Correct 15 ms 7072 KB answer = YES
52 Correct 13 ms 6996 KB answer = NO
53 Correct 4 ms 5204 KB answer = YES
54 Correct 5 ms 5332 KB answer = YES
55 Correct 10 ms 5784 KB answer = YES
56 Correct 13 ms 6500 KB answer = YES
57 Correct 13 ms 6324 KB answer = YES
58 Correct 12 ms 6220 KB answer = YES
59 Correct 12 ms 6228 KB answer = YES
60 Correct 14 ms 6464 KB answer = YES
61 Correct 8 ms 5716 KB answer = YES
62 Correct 191 ms 22128 KB answer = NO
63 Correct 232 ms 23116 KB answer = YES
64 Correct 228 ms 23116 KB answer = NO
65 Correct 223 ms 23124 KB answer = YES
66 Correct 4 ms 5332 KB answer = YES
67 Correct 116 ms 29508 KB answer = YES
68 Correct 112 ms 29532 KB answer = YES
69 Correct 372 ms 29204 KB answer = YES
70 Correct 195 ms 40788 KB answer = YES
71 Correct 346 ms 29472 KB answer = YES
72 Correct 180 ms 19092 KB answer = YES
73 Correct 187 ms 17964 KB answer = YES
74 Correct 89 ms 18760 KB answer = YES
75 Correct 69 ms 19344 KB answer = NO
76 Correct 17 ms 6776 KB answer = YES
77 Correct 32 ms 8596 KB answer = YES
78 Correct 57 ms 11104 KB answer = YES
79 Correct 142 ms 16964 KB answer = YES
80 Correct 100 ms 19516 KB answer = YES
81 Correct 228 ms 23204 KB answer = NO
82 Correct 287 ms 28140 KB answer = YES
83 Correct 465 ms 29384 KB answer = YES
84 Correct 187 ms 29556 KB answer = YES
85 Correct 115 ms 29540 KB answer = YES
86 Correct 362 ms 29368 KB answer = YES
87 Correct 183 ms 21020 KB answer = NO
88 Correct 406 ms 23460 KB answer = YES
89 Correct 566 ms 17664 KB answer = YES
90 Correct 547 ms 17728 KB answer = YES
91 Correct 548 ms 17816 KB answer = YES
92 Correct 72 ms 11648 KB answer = YES
93 Correct 77 ms 11468 KB answer = YES
94 Correct 145 ms 29200 KB answer = NO
95 Correct 138 ms 16676 KB answer = NO
96 Correct 367 ms 33772 KB answer = YES
97 Correct 83 ms 29192 KB answer = NO