Submission #369910

# Submission time Handle Problem Language Result Execution time Memory
369910 2021-02-22T17:55:24 Z ivan_tudor Love Polygon (BOI18_polygon) C++14
100 / 100
511 ms 35044 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
map<string, int> id;
map<pair<int, int>, int> edges;
vector<int> gr[N];
int mchtree[N][2];
bool viz[N];
void dfsdown(int nod,int dad1,int dad2){
  viz[nod] = true;
  int maxdif = INT_MIN;
  for(int x:gr[nod]){
    if(x!=dad1 && x!=dad2 && x!= nod){
      dfsdown(x, nod, 0);
      mchtree[nod][0] += max(mchtree[x][0], mchtree[x][1]);
      maxdif = max(maxdif, mchtree[x][0] + 1 - max(mchtree[x][0], mchtree[x][1]));
    }
  }
  if(maxdif != INT_MIN)
    mchtree[nod][1] = mchtree[nod][0] + maxdif;
}

stack<int> activ;
int dfs_cycle(int nod, int prev){
  activ.push(nod);
  viz[nod] = true;
  for(auto x:gr[nod]){
    if(viz[x] == false){
      int val = dfs_cycle(x, nod);
      if(val != -1)
        return val;
    }
    else if((x!= prev && viz[x] == true) || (x == prev && edges[{min(x, nod), max(x, nod)}] == 2)){
      return x;
    }
  }
  activ.pop();
  return -1;
}
int mch[N][2];
int makedp(vector<int> &cycle, int l,int r){
  if(l > r)
    return 0;
  mch[l][0] = mchtree[cycle[l]][0], mch[l][1] = mchtree[cycle[l]][1];
  for(int i=l + 1; i<=r;i++){
    int nod = cycle[i];
    mch[i][1] = max(mchtree[nod][1] + max(mch[i-1][0], mch[i-1][1]), mchtree[nod][0] + 1 + mch[i-1][0]);
    mch[i][0] = max(mch[i-1][0], mch[i-1][1]) + mchtree[nod][0];
  }
  return max(mch[r][0], mch[r][1]);
}
int solve_cycle(vector<int> &cycle){
  int csize = cycle.size();
  for(int i=0;i<cycle.size();i++){
    int next = cycle[(i + 1)%csize], prev = cycle[(i - 1 + csize)%csize];
    dfsdown(cycle[i], next, prev);
  }
  int answer = 0;
  // pot lua 0 - csize -1; 0 -1; 0 blocat din copac sau gol
  int ansc = 1 + mchtree[cycle[0]][0] + mchtree[cycle[csize - 1]][0] + makedp(cycle, 1, csize - 2);
  if(csize == 2)
    ansc++; // caz part
  answer = max(answer, ansc);

  ansc = 1 + mchtree[cycle[0]][0] + mchtree[cycle[1]][0] + makedp(cycle, 2, csize - 1);
  if(csize == 2)
    ansc++;
  answer = max(answer, ansc);

  ansc = max(mchtree[cycle[0]][0], mchtree[cycle[0]][1]) + makedp(cycle, 1, csize - 1);
  answer = max(answer, ansc);
  return answer;
}
int main()
{
  //freopen(".in","r",stdin);
  ios::sync_with_stdio(false);
  cin.tie(0),cout.tie(0);
  int n = 0;
  int m;
  cin>>m;
  for(int i=1;i<=m;i++){
    string s;
    int x, y;
    cin>>s;
    if(!id.count(s))
      id[s] = ++n;
    x = id[s];
    cin>>s;
    if(!id.count(s))
      id[s] = ++n;
    y = id[s];
    if(x > y)
      swap(x, y);
    if(edges.count({x, y})){
      edges[{x, y}]++;
      continue;
    }
    if(x != y){
      gr[x].push_back(y);
      gr[y].push_back(x);
    }
    edges[{x,y}] = 1;
  }
  if(n%2 == 1){
    cout<<"-1";
    return 0;
  }
  int answer = 0;
  for(int i=1;i<=n;i++){
    if(viz[i] == false){
      activ = stack<int>();
      int valcyc = dfs_cycle(i, 0);
      if(valcyc == -1){
        dfsdown(i, 0, 0);
        answer+= max(mchtree[i][0], mchtree[i][1]);
      }
      else{
        vector<int> cycle;
        do{
          cycle.push_back(activ.top());
          activ.pop();
        }while(cycle.end()[-1] != valcyc);
        answer+= solve_cycle(cycle);
      }
    }
  }
  cout<< n - answer;
  return 0;
}

Compilation message

polygon.cpp: In function 'int solve_cycle(std::vector<int>&)':
polygon.cpp:54:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |   for(int i=0;i<cycle.size();i++){
      |               ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2668 KB Output is correct
8 Correct 2 ms 2668 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Correct 2 ms 2668 KB Output is correct
11 Correct 2 ms 2668 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2668 KB Output is correct
14 Correct 2 ms 2668 KB Output is correct
15 Correct 3 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 445 ms 30956 KB Output is correct
5 Correct 439 ms 19820 KB Output is correct
6 Correct 457 ms 32868 KB Output is correct
7 Correct 390 ms 19968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 463 ms 20796 KB Output is correct
2 Correct 478 ms 22904 KB Output is correct
3 Correct 263 ms 16876 KB Output is correct
4 Correct 453 ms 20076 KB Output is correct
5 Correct 464 ms 27372 KB Output is correct
6 Correct 450 ms 20884 KB Output is correct
7 Correct 431 ms 20844 KB Output is correct
8 Correct 379 ms 20048 KB Output is correct
9 Correct 439 ms 21228 KB Output is correct
10 Correct 267 ms 20440 KB Output is correct
11 Correct 2 ms 2668 KB Output is correct
12 Correct 2 ms 2796 KB Output is correct
13 Correct 2 ms 2668 KB Output is correct
14 Correct 2 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2668 KB Output is correct
8 Correct 2 ms 2668 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Correct 2 ms 2668 KB Output is correct
11 Correct 2 ms 2668 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2668 KB Output is correct
14 Correct 2 ms 2668 KB Output is correct
15 Correct 3 ms 2668 KB Output is correct
16 Correct 2 ms 2668 KB Output is correct
17 Correct 2 ms 2668 KB Output is correct
18 Correct 2 ms 2668 KB Output is correct
19 Correct 445 ms 30956 KB Output is correct
20 Correct 439 ms 19820 KB Output is correct
21 Correct 457 ms 32868 KB Output is correct
22 Correct 390 ms 19968 KB Output is correct
23 Correct 463 ms 20796 KB Output is correct
24 Correct 478 ms 22904 KB Output is correct
25 Correct 263 ms 16876 KB Output is correct
26 Correct 453 ms 20076 KB Output is correct
27 Correct 464 ms 27372 KB Output is correct
28 Correct 450 ms 20884 KB Output is correct
29 Correct 431 ms 20844 KB Output is correct
30 Correct 379 ms 20048 KB Output is correct
31 Correct 439 ms 21228 KB Output is correct
32 Correct 267 ms 20440 KB Output is correct
33 Correct 2 ms 2668 KB Output is correct
34 Correct 2 ms 2796 KB Output is correct
35 Correct 2 ms 2668 KB Output is correct
36 Correct 2 ms 2668 KB Output is correct
37 Correct 430 ms 23148 KB Output is correct
38 Correct 439 ms 25196 KB Output is correct
39 Correct 428 ms 23020 KB Output is correct
40 Correct 422 ms 23148 KB Output is correct
41 Correct 429 ms 23148 KB Output is correct
42 Correct 459 ms 23020 KB Output is correct
43 Correct 444 ms 23148 KB Output is correct
44 Correct 429 ms 23144 KB Output is correct
45 Correct 471 ms 23052 KB Output is correct
46 Correct 475 ms 23148 KB Output is correct
47 Correct 421 ms 23532 KB Output is correct
48 Correct 511 ms 23020 KB Output is correct
49 Correct 465 ms 24940 KB Output is correct
50 Correct 258 ms 19180 KB Output is correct
51 Correct 404 ms 22380 KB Output is correct
52 Correct 470 ms 29528 KB Output is correct
53 Correct 441 ms 23020 KB Output is correct
54 Correct 444 ms 23148 KB Output is correct
55 Correct 344 ms 22124 KB Output is correct
56 Correct 446 ms 23660 KB Output is correct
57 Correct 269 ms 22628 KB Output is correct
58 Correct 2 ms 2668 KB Output is correct
59 Correct 2 ms 2668 KB Output is correct
60 Correct 2 ms 2668 KB Output is correct
61 Correct 2 ms 2668 KB Output is correct
62 Correct 2 ms 2668 KB Output is correct
63 Correct 2 ms 2668 KB Output is correct
64 Correct 2 ms 2668 KB Output is correct
65 Correct 434 ms 33132 KB Output is correct
66 Correct 437 ms 22124 KB Output is correct
67 Correct 435 ms 35044 KB Output is correct
68 Correct 388 ms 22208 KB Output is correct
69 Correct 2 ms 2668 KB Output is correct
70 Correct 2 ms 2668 KB Output is correct
71 Correct 2 ms 2668 KB Output is correct
72 Correct 2 ms 2668 KB Output is correct
73 Correct 2 ms 2668 KB Output is correct
74 Correct 2 ms 2668 KB Output is correct
75 Correct 2 ms 2668 KB Output is correct
76 Correct 2 ms 2668 KB Output is correct
77 Correct 2 ms 2668 KB Output is correct
78 Correct 2 ms 2668 KB Output is correct
79 Correct 2 ms 2668 KB Output is correct
80 Correct 2 ms 2668 KB Output is correct
81 Correct 2 ms 2668 KB Output is correct
82 Correct 2 ms 2668 KB Output is correct
83 Correct 2 ms 2668 KB Output is correct