Submission #576822

# Submission time Handle Problem Language Result Execution time Memory
576822 2022-06-13T15:12:42 Z Sam_a17 Love Polygon (BOI18_polygon) C++14
100 / 100
107 ms 22604 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++;

    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);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 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 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 96 ms 21564 KB Output is correct
5 Correct 85 ms 14796 KB Output is correct
6 Correct 86 ms 22556 KB Output is correct
7 Correct 2 ms 4180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 14904 KB Output is correct
2 Correct 78 ms 14588 KB Output is correct
3 Correct 51 ms 14744 KB Output is correct
4 Correct 3 ms 4180 KB Output is correct
5 Correct 76 ms 14420 KB Output is correct
6 Correct 66 ms 14728 KB Output is correct
7 Correct 64 ms 14948 KB Output is correct
8 Correct 61 ms 15052 KB Output is correct
9 Correct 63 ms 14796 KB Output is correct
10 Correct 43 ms 14460 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 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 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 96 ms 21564 KB Output is correct
20 Correct 85 ms 14796 KB Output is correct
21 Correct 86 ms 22556 KB Output is correct
22 Correct 2 ms 4180 KB Output is correct
23 Correct 69 ms 14904 KB Output is correct
24 Correct 78 ms 14588 KB Output is correct
25 Correct 51 ms 14744 KB Output is correct
26 Correct 3 ms 4180 KB Output is correct
27 Correct 76 ms 14420 KB Output is correct
28 Correct 66 ms 14728 KB Output is correct
29 Correct 64 ms 14948 KB Output is correct
30 Correct 61 ms 15052 KB Output is correct
31 Correct 63 ms 14796 KB Output is correct
32 Correct 43 ms 14460 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
34 Correct 1 ms 2644 KB Output is correct
35 Correct 1 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 64 ms 14540 KB Output is correct
38 Correct 69 ms 14620 KB Output is correct
39 Correct 71 ms 15096 KB Output is correct
40 Correct 73 ms 14720 KB Output is correct
41 Correct 74 ms 14612 KB Output is correct
42 Correct 73 ms 14584 KB Output is correct
43 Correct 87 ms 14600 KB Output is correct
44 Correct 83 ms 14628 KB Output is correct
45 Correct 74 ms 14564 KB Output is correct
46 Correct 64 ms 14636 KB Output is correct
47 Correct 59 ms 14772 KB Output is correct
48 Correct 84 ms 15028 KB Output is correct
49 Correct 87 ms 14496 KB Output is correct
50 Correct 44 ms 14804 KB Output is correct
51 Correct 2 ms 4180 KB Output is correct
52 Correct 71 ms 14472 KB Output is correct
53 Correct 68 ms 14636 KB Output is correct
54 Correct 94 ms 14952 KB Output is correct
55 Correct 72 ms 15056 KB Output is correct
56 Correct 53 ms 14824 KB Output is correct
57 Correct 43 ms 14448 KB Output is correct
58 Correct 1 ms 2644 KB Output is correct
59 Correct 1 ms 2644 KB Output is correct
60 Correct 1 ms 2644 KB Output is correct
61 Correct 1 ms 2644 KB Output is correct
62 Correct 2 ms 2644 KB Output is correct
63 Correct 1 ms 2644 KB Output is correct
64 Correct 1 ms 2644 KB Output is correct
65 Correct 84 ms 21580 KB Output is correct
66 Correct 97 ms 14900 KB Output is correct
67 Correct 107 ms 22604 KB Output is correct
68 Correct 2 ms 4180 KB Output is correct
69 Correct 1 ms 2644 KB Output is correct
70 Correct 2 ms 2644 KB Output is correct
71 Correct 1 ms 2644 KB Output is correct
72 Correct 1 ms 2644 KB Output is correct
73 Correct 1 ms 2644 KB Output is correct
74 Correct 1 ms 2644 KB Output is correct
75 Correct 1 ms 2644 KB Output is correct
76 Correct 2 ms 2644 KB Output is correct
77 Correct 2 ms 2644 KB Output is correct
78 Correct 1 ms 2644 KB Output is correct
79 Correct 2 ms 2644 KB Output is correct
80 Correct 1 ms 2644 KB Output is correct
81 Correct 1 ms 2644 KB Output is correct
82 Correct 1 ms 2644 KB Output is correct
83 Correct 2 ms 2644 KB Output is correct