Submission #1041089

# Submission time Handle Problem Language Result Execution time Memory
1041089 2024-08-01T15:20:15 Z jer033 Parachute rings (IOI12_rings) C++17
100 / 100
552 ms 244972 KB
#include <bits/stdc++.h>
using namespace std;

int N;
int curr_answer;
bool second_stage;

class LinkedIn{
public:
    vector<vector<int>> edges;
    vector<int> chains;
    //value encoded: other endpoint of chain (itself if 0 connections), or -1 if it has 2 connections
    vector<int> degree;
    int cycle_count;
    int cycle_size;
    bool three_exists;

    LinkedIn(int N_)
    {
        edges = vector<vector<int>> (N_);
        chains = vector<int> (N_);
        for (int i=0; i<N_; i++)
            chains[i] = i;
        degree = vector<int> (N_, 0);
        
        cycle_count = 0;
        cycle_size = 0;
        three_exists = 0;
    };

    int CreateLink(int A, int B)
    {
        degree[A]++;
        degree[B]++;
        
        if ((chains[A]>=0) and (chains[B]>=0))
        {
            if ((chains[A]==B) and (chains[B]==A))
            {
                cycle_count++;
                cycle_count = min(cycle_count, 2);
                chains[A] = -1;
                chains[B] = -1;
            }
            else
            {
                int newchainsA, newchainsB;
                int newchains2A, newchains2B;
                if (chains[A]==A)
                    newchainsA = chains[B];
                else
                {
                    newchains2A = chains[B];
                    newchainsA = -1;
                }
                if (chains[B]==B)
                    newchainsB = chains[A];
                else
                {
                    newchains2B = chains[A];
                    newchainsB = -1;
                }

                if (chains[A]==A)
                    chains[A] = newchainsA;
                else
                {
                    chains[chains[A]] = newchains2A;
                    chains[A] = newchainsA;
                }

                if (chains[B]==B)
                    chains[B] = newchainsB;
                else
                {
                    chains[chains[B]] = newchains2B;
                    chains[B] = newchainsB;
                }
            }
        }
        else{
            three_exists = 1;
        }

        if (degree[A]>=3)
            three_exists = 1;
        if (degree[B]>=3)
            three_exists = 1;

        /*for (int i=0; i<N; i++)
            cout << chains[i] << ' ';
        cout << '\n';*/

        if (three_exists)
            return 3;
        else if (cycle_count > 0)
            return min(cycle_count, 2);
        return 0;
    };

    int CCreateLink(int A, int B)
    {
        degree[A]++;
        degree[B]++;
        
        if ((chains[A]>=0) and (chains[B]>=0))
        {
            if ((chains[A]==B) and (chains[B]==A))
            {
                cycle_count++;
                cycle_count = min(cycle_count, 2);
                if (cycle_count == 1)
                {
                    vector<int> visited(N, 0);
                    visited[A] = 1;
                    int curr = A;
                    cycle_size = 1;
                    while (curr!=B)
                    {
                        int next_node;
                        for (int c: edges[curr])
                            if (visited[c]==0)
                                next_node = c;
                        visited[next_node] = 1;
                        curr = next_node;
                        cycle_size++;
                    }
                    //cout << "A cycle of size " << cycle_size << " has been formed\n";
                }
                chains[A] = -1;
                chains[B] = -1;
            }
            else
            {
                int newchainsA, newchainsB;
                int newchains2A, newchains2B;
                if (chains[A]==A)
                    newchainsA = chains[B];
                else
                {
                    newchains2A = chains[B];
                    newchainsA = -1;
                }
                if (chains[B]==B)
                    newchainsB = chains[A];
                else
                {
                    newchains2B = chains[A];
                    newchainsB = -1;
                }

                if (chains[A]==A)
                    chains[A] = newchainsA;
                else
                {
                    chains[chains[A]] = newchains2A;
                    chains[A] = newchainsA;
                }

                if (chains[B]==B)
                    chains[B] = newchainsB;
                else
                {
                    chains[chains[B]] = newchains2B;
                    chains[B] = newchainsB;
                }
            }
        }
        else{
            three_exists = 1;
        }

        edges[A].push_back(B);
        edges[B].push_back(A);

        if (degree[A]>=3)
            three_exists = 1;
        if (degree[B]>=3)
            three_exists = 1;

        /*for (int i=0; i<N; i++)
            cout << chains[i] << ' ';
        cout << '\n';*/

        if (three_exists)
            return 3;
        else if (cycle_count > 0)
            return min(cycle_count, 2);
        return 0;
    };

    int answer()
    {
        if (cycle_count>=2)
            return 0;
        if (cycle_count==1)
            return cycle_size;
        return N;
    }

    int answer_2()
    {
        if (three_exists)
            return 0;
        if (cycle_count >= 1)
            return 0;
        return 1;
    }
};

int c1, c2, c3, c4;
LinkedIn sys_0(0);
LinkedIn sys_1(0);
LinkedIn sys_2(0);
LinkedIn sys_3(0);
LinkedIn sys_4(0);

void Init(int N_) {
    N = N_;
    curr_answer = N_;
    sys_0 = LinkedIn(N_);
    second_stage = 0;
}

void Link(int A, int B) {
    if (second_stage == 0)
    {
        int result = sys_0.CCreateLink(A, B);
        if (result != 3)
        {
            curr_answer = sys_0.answer();
        }
        else
        {
            second_stage = 1;
            vector<vector<int>> con = sys_0.edges;
            vector<int> deg = sys_0.degree;
            int center;
            if (deg[A] == 3)
                center = A;
            else
                center = B;
            c1 = center;
            c2 = con[center][0];
            c3 = con[center][1];
            c4 = con[center][2];
            sys_1 = LinkedIn(N);
            sys_2 = LinkedIn(N);
            sys_3 = LinkedIn(N);
            sys_4 = LinkedIn(N);
            //cout << c1 << c2 << c3 << c4 << '\n';
            for (int i=0; i<N; i++)
                for (int j: con[i])
                    if (i<j)
                    {
                        if ((i!=c1) and (j!=c1))
                            int x = sys_1.CreateLink(i, j);
                        if ((i!=c2) and (j!=c2))
                            int x = sys_2.CreateLink(i, j);
                        if ((i!=c3) and (j!=c3))
                            int x = sys_3.CreateLink(i, j);
                        if ((i!=c4) and (j!=c4))
                            int x = sys_4.CreateLink(i, j);
                    }
            curr_answer = sys_1.answer_2() + sys_2.answer_2() + sys_3.answer_2() + sys_4.answer_2();
        }
    }
    else
    {
        int i = A;
        int j = B;
        if ((i!=c1) and (j!=c1))
            int x = sys_1.CreateLink(i, j);
        if ((i!=c2) and (j!=c2))
            int x = sys_2.CreateLink(i, j);
        if ((i!=c3) and (j!=c3))
            int x = sys_3.CreateLink(i, j);
        if ((i!=c4) and (j!=c4))
            int x = sys_4.CreateLink(i, j);
                    
        curr_answer = sys_1.answer_2() + sys_2.answer_2() + sys_3.answer_2() + sys_4.answer_2();
    }
}

int CountCritical() {
    return curr_answer;
}

/*int main()
{
    Init(1'000'000);
    for (int i=0; i<50'000; i++)
    {
        Link(i, i+1);
    }
    Link(76892, 291238);
    Link(4, 100);
    cout << CountCritical() << '\n';
}*/

Compilation message

rings.cpp: In function 'void Link(int, int)':
rings.cpp:257:33: warning: unused variable 'x' [-Wunused-variable]
  257 |                             int x = sys_1.CreateLink(i, j);
      |                                 ^
rings.cpp:259:33: warning: unused variable 'x' [-Wunused-variable]
  259 |                             int x = sys_2.CreateLink(i, j);
      |                                 ^
rings.cpp:261:33: warning: unused variable 'x' [-Wunused-variable]
  261 |                             int x = sys_3.CreateLink(i, j);
      |                                 ^
rings.cpp:263:33: warning: unused variable 'x' [-Wunused-variable]
  263 |                             int x = sys_4.CreateLink(i, j);
      |                                 ^
rings.cpp:273:17: warning: unused variable 'x' [-Wunused-variable]
  273 |             int x = sys_1.CreateLink(i, j);
      |                 ^
rings.cpp:275:17: warning: unused variable 'x' [-Wunused-variable]
  275 |             int x = sys_2.CreateLink(i, j);
      |                 ^
rings.cpp:277:17: warning: unused variable 'x' [-Wunused-variable]
  277 |             int x = sys_3.CreateLink(i, j);
      |                 ^
rings.cpp:279:17: warning: unused variable 'x' [-Wunused-variable]
  279 |             int x = sys_4.CreateLink(i, j);
      |                 ^
rings.cpp: In member function 'int LinkedIn::CCreateLink(int, int)':
rings.cpp:164:39: warning: 'newchains2B' may be used uninitialized in this function [-Wmaybe-uninitialized]
  164 |                     chains[chains[B]] = newchains2B;
rings.cpp:124:42: warning: 'next_node' may be used uninitialized in this function [-Wmaybe-uninitialized]
  124 |                         visited[next_node] = 1;
      |                                          ^
rings.cpp: In function 'void Link(int, int)':
rings.cpp:76:39: warning: 'newchains2B' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |                     chains[chains[B]] = newchains2B;
rings.cpp:48:34: note: 'newchains2B' was declared here
   48 |                 int newchains2A, newchains2B;
      |                                  ^~~~~~~~~~~
rings.cpp:76:39: warning: 'newchains2B' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |                     chains[chains[B]] = newchains2B;
rings.cpp:48:34: note: 'newchains2B' was declared here
   48 |                 int newchains2A, newchains2B;
      |                                  ^~~~~~~~~~~
rings.cpp:76:39: warning: 'newchains2B' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |                     chains[chains[B]] = newchains2B;
rings.cpp:48:34: note: 'newchains2B' was declared here
   48 |                 int newchains2A, newchains2B;
      |                                  ^~~~~~~~~~~
rings.cpp:76:39: warning: 'newchains2B' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |                     chains[chains[B]] = newchains2B;
rings.cpp:48:34: note: 'newchains2B' was declared here
   48 |                 int newchains2A, newchains2B;
      |                                  ^~~~~~~~~~~
rings.cpp:76:39: warning: 'newchains2B' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |                     chains[chains[B]] = newchains2B;
rings.cpp:48:34: note: 'newchains2B' was declared here
   48 |                 int newchains2A, newchains2B;
      |                                  ^~~~~~~~~~~
rings.cpp:76:39: warning: 'newchains2B' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |                     chains[chains[B]] = newchains2B;
rings.cpp:48:34: note: 'newchains2B' was declared here
   48 |                 int newchains2A, newchains2B;
      |                                  ^~~~~~~~~~~
rings.cpp:76:39: warning: 'newchains2B' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |                     chains[chains[B]] = newchains2B;
rings.cpp:48:34: note: 'newchains2B' was declared here
   48 |                 int newchains2A, newchains2B;
      |                                  ^~~~~~~~~~~
rings.cpp:76:39: warning: 'newchains2B' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |                     chains[chains[B]] = newchains2B;
rings.cpp:48:34: note: 'newchains2B' was declared here
   48 |                 int newchains2A, newchains2B;
      |                                  ^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 1372 KB Output is correct
3 Correct 1 ms 1372 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 1372 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 1368 KB Output is correct
10 Correct 1 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 146 ms 32852 KB Output is correct
2 Correct 347 ms 175444 KB Output is correct
3 Correct 302 ms 185168 KB Output is correct
4 Correct 417 ms 66896 KB Output is correct
5 Correct 432 ms 66772 KB Output is correct
6 Correct 511 ms 65492 KB Output is correct
7 Correct 286 ms 185428 KB Output is correct
8 Correct 470 ms 224848 KB Output is correct
9 Correct 552 ms 244972 KB Output is correct
10 Correct 285 ms 64936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 1372 KB Output is correct
3 Correct 1 ms 1372 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 1372 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 1368 KB Output is correct
10 Correct 1 ms 1372 KB Output is correct
11 Correct 2 ms 1372 KB Output is correct
12 Correct 3 ms 2908 KB Output is correct
13 Correct 3 ms 2652 KB Output is correct
14 Correct 2 ms 2140 KB Output is correct
15 Correct 2 ms 4188 KB Output is correct
16 Correct 2 ms 1116 KB Output is correct
17 Correct 2 ms 2140 KB Output is correct
18 Correct 3 ms 4188 KB Output is correct
19 Correct 2 ms 1008 KB Output is correct
20 Correct 3 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 1372 KB Output is correct
3 Correct 1 ms 1372 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 1372 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 1368 KB Output is correct
10 Correct 1 ms 1372 KB Output is correct
11 Correct 2 ms 1372 KB Output is correct
12 Correct 3 ms 2908 KB Output is correct
13 Correct 3 ms 2652 KB Output is correct
14 Correct 2 ms 2140 KB Output is correct
15 Correct 2 ms 4188 KB Output is correct
16 Correct 2 ms 1116 KB Output is correct
17 Correct 2 ms 2140 KB Output is correct
18 Correct 3 ms 4188 KB Output is correct
19 Correct 2 ms 1008 KB Output is correct
20 Correct 3 ms 2652 KB Output is correct
21 Correct 10 ms 2908 KB Output is correct
22 Correct 15 ms 4444 KB Output is correct
23 Correct 19 ms 5468 KB Output is correct
24 Correct 19 ms 17500 KB Output is correct
25 Correct 13 ms 18776 KB Output is correct
26 Correct 20 ms 17800 KB Output is correct
27 Correct 25 ms 5652 KB Output is correct
28 Correct 19 ms 16896 KB Output is correct
29 Correct 18 ms 19036 KB Output is correct
30 Correct 28 ms 6992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 1372 KB Output is correct
3 Correct 1 ms 1372 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 1372 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 1368 KB Output is correct
10 Correct 1 ms 1372 KB Output is correct
11 Correct 146 ms 32852 KB Output is correct
12 Correct 347 ms 175444 KB Output is correct
13 Correct 302 ms 185168 KB Output is correct
14 Correct 417 ms 66896 KB Output is correct
15 Correct 432 ms 66772 KB Output is correct
16 Correct 511 ms 65492 KB Output is correct
17 Correct 286 ms 185428 KB Output is correct
18 Correct 470 ms 224848 KB Output is correct
19 Correct 552 ms 244972 KB Output is correct
20 Correct 285 ms 64936 KB Output is correct
21 Correct 2 ms 1372 KB Output is correct
22 Correct 3 ms 2908 KB Output is correct
23 Correct 3 ms 2652 KB Output is correct
24 Correct 2 ms 2140 KB Output is correct
25 Correct 2 ms 4188 KB Output is correct
26 Correct 2 ms 1116 KB Output is correct
27 Correct 2 ms 2140 KB Output is correct
28 Correct 3 ms 4188 KB Output is correct
29 Correct 2 ms 1008 KB Output is correct
30 Correct 3 ms 2652 KB Output is correct
31 Correct 10 ms 2908 KB Output is correct
32 Correct 15 ms 4444 KB Output is correct
33 Correct 19 ms 5468 KB Output is correct
34 Correct 19 ms 17500 KB Output is correct
35 Correct 13 ms 18776 KB Output is correct
36 Correct 20 ms 17800 KB Output is correct
37 Correct 25 ms 5652 KB Output is correct
38 Correct 19 ms 16896 KB Output is correct
39 Correct 18 ms 19036 KB Output is correct
40 Correct 28 ms 6992 KB Output is correct
41 Correct 82 ms 28116 KB Output is correct
42 Correct 273 ms 182356 KB Output is correct
43 Correct 122 ms 170580 KB Output is correct
44 Correct 228 ms 170664 KB Output is correct
45 Correct 235 ms 186192 KB Output is correct
46 Correct 274 ms 65796 KB Output is correct
47 Correct 432 ms 68432 KB Output is correct
48 Correct 184 ms 186192 KB Output is correct
49 Correct 263 ms 67636 KB Output is correct
50 Correct 302 ms 66680 KB Output is correct
51 Correct 119 ms 145756 KB Output is correct
52 Correct 194 ms 135968 KB Output is correct
53 Correct 179 ms 184660 KB Output is correct
54 Correct 394 ms 191060 KB Output is correct
55 Correct 300 ms 162640 KB Output is correct