답안 #576808

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
576808 2022-06-13T14:41:57 Z Sam_a17 Love Polygon (BOI18_polygon) C++14
100 / 100
107 ms 24768 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 blt __builtin_popcount

#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, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } 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 = 1e5 + 10;
int n, it = 1, ci = 1, answ, kent;
unordered_map<string, int> mp;
vector<int> adj[N];
int used[N], p[N], in[N];
bool happy[N];

void dfsCycle(int node, int parent) {
  used[node] = 1, p[node] = parent;
  for(auto i: adj[node]) {
    if(i == parent) continue;

    if(used[i] == 1) {
      int curr = node, cnt = 1;

      while(curr != i) {
        curr = p[curr];
        cnt++;
      }
      
      answ += cnt / 2;
      if(cnt & 1) {
        kent++;
      }
    } else if(!used[i]) {
      dfsCycle(i, node);
    }
  }

  used[node] = 2;
}

void solve_() {
  cin >> n, mp.reserve(2 * n);

  if(n & 1) {
    printf("-1\n");
    return;
  }

  for(int i = 1; i <= n; i++) {
    string a, b; cin >> a >> b;

    int ida, idb;

    if(mp.find(a) == mp.end()) {
      mp[a] = it++;
      ida = it - 1;
    } else {
      ida = mp[a];
    }

    if(mp.find(b) == mp.end()) {
      mp[b] = it++;
      idb = it - 1;
    } else {
      idb = mp[b];
    }

    adj[ida].push_back(idb);
    p[ida] = idb;
    in[idb]++;

    if(ida != idb && p[idb] == ida) {
      happy[ida] = happy[idb] = true;
    }
  }

  assert(it == n + 1);

  queue<int> q;
  for(int i = 1; i <= n; i++) {
    if(!in[i]) {
      q.push(i);
    }
  }

  while(!q.empty()) {
    auto u = q.front();
    q.pop();

    if(happy[p[u]]) {
      happy[u] = true;
      kent++;
      continue;
    }

    happy[u] = happy[p[u]] = true;
    answ++;

    if (!happy[p[p[u]]]) {
      in[p[p[u]]]--;
      if (!in[p[p[u]]]) {
        q.push(p[p[u]]);
      }
    }
  }

  for(int i = 1; i <= n; i++) {
    if(!happy[i]) {
      // cycles
      dfsCycle(i, 0);
    }
  }

  assert(kent % 2 == 0);
  answ += kent;

  printf("%d\n", answ);
} 

int main() {
  setIO("");

  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };

  int test_cases = 1;
  solve(test_cases);

  return 0;
}

Compilation message

polygon.cpp: In function 'void setIO(std::string)':
polygon.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 + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
polygon.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2592 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 88 ms 23736 KB Output is correct
5 Correct 83 ms 16988 KB Output is correct
6 Correct 107 ms 24768 KB Output is correct
7 Correct 3 ms 4232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 15648 KB Output is correct
2 Correct 79 ms 16656 KB Output is correct
3 Correct 46 ms 16908 KB Output is correct
4 Correct 2 ms 4308 KB Output is correct
5 Correct 80 ms 16636 KB Output is correct
6 Correct 71 ms 16836 KB Output is correct
7 Correct 67 ms 17164 KB Output is correct
8 Correct 66 ms 17144 KB Output is correct
9 Correct 64 ms 16984 KB Output is correct
10 Correct 46 ms 16672 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2592 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 88 ms 23736 KB Output is correct
20 Correct 83 ms 16988 KB Output is correct
21 Correct 107 ms 24768 KB Output is correct
22 Correct 3 ms 4232 KB Output is correct
23 Correct 87 ms 15648 KB Output is correct
24 Correct 79 ms 16656 KB Output is correct
25 Correct 46 ms 16908 KB Output is correct
26 Correct 2 ms 4308 KB Output is correct
27 Correct 80 ms 16636 KB Output is correct
28 Correct 71 ms 16836 KB Output is correct
29 Correct 67 ms 17164 KB Output is correct
30 Correct 66 ms 17144 KB Output is correct
31 Correct 64 ms 16984 KB Output is correct
32 Correct 46 ms 16672 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 1 ms 2644 KB Output is correct
37 Correct 77 ms 16708 KB Output is correct
38 Correct 71 ms 16768 KB Output is correct
39 Correct 77 ms 17156 KB Output is correct
40 Correct 64 ms 16892 KB Output is correct
41 Correct 64 ms 16844 KB Output is correct
42 Correct 69 ms 16808 KB Output is correct
43 Correct 90 ms 16844 KB Output is correct
44 Correct 89 ms 16856 KB Output is correct
45 Correct 64 ms 16732 KB Output is correct
46 Correct 68 ms 16840 KB Output is correct
47 Correct 58 ms 16912 KB Output is correct
48 Correct 75 ms 17104 KB Output is correct
49 Correct 72 ms 16672 KB Output is correct
50 Correct 44 ms 16972 KB Output is correct
51 Correct 2 ms 4308 KB Output is correct
52 Correct 71 ms 16632 KB Output is correct
53 Correct 65 ms 16844 KB Output is correct
54 Correct 91 ms 17100 KB Output is correct
55 Correct 67 ms 17212 KB Output is correct
56 Correct 57 ms 16872 KB Output is correct
57 Correct 47 ms 16628 KB Output is correct
58 Correct 2 ms 2644 KB Output is correct
59 Correct 1 ms 2644 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
61 Correct 2 ms 2644 KB Output is correct
62 Correct 2 ms 2684 KB Output is correct
63 Correct 2 ms 2644 KB Output is correct
64 Correct 2 ms 2644 KB Output is correct
65 Correct 88 ms 23772 KB Output is correct
66 Correct 76 ms 17000 KB Output is correct
67 Correct 88 ms 24712 KB Output is correct
68 Correct 2 ms 4308 KB Output is correct
69 Correct 2 ms 2684 KB Output is correct
70 Correct 1 ms 2644 KB Output is correct
71 Correct 2 ms 2644 KB Output is correct
72 Correct 1 ms 2644 KB Output is correct
73 Correct 2 ms 2644 KB Output is correct
74 Correct 2 ms 2644 KB Output is correct
75 Correct 2 ms 2644 KB Output is correct
76 Correct 1 ms 2684 KB Output is correct
77 Correct 1 ms 2644 KB Output is correct
78 Correct 2 ms 2684 KB Output is correct
79 Correct 1 ms 2644 KB Output is correct
80 Correct 2 ms 2684 KB Output is correct
81 Correct 2 ms 2684 KB Output is correct
82 Correct 1 ms 2644 KB Output is correct
83 Correct 1 ms 2644 KB Output is correct