Submission #434835

# Submission time Handle Problem Language Result Execution time Memory
434835 2021-06-21T22:34:52 Z 2qbingxuan Parachute rings (IOI12_rings) C++14
20 / 100
4000 ms 1356 KB
#include <bits/stdc++.h>
#ifdef local
#define debug(x...) qqbx(#x, x)
#define pary(x...) danb(#x, x)
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
template <typename ...T> void qqbx(const char *s, T ...a) {
    int cnt = sizeof...(T);
    ((std::cerr << "(" << s << ") = ("), ..., (std::cerr << a << (--cnt ? ", " : ")\n")));
}
template <typename T> void danb(const char *t, T L, T R) {
    std::cerr << "[ " << t << " ] = [ ";
    for (int f = 0; L != R; ++L) std::cerr << (f++ ? ", " : "") << *L;
    std::cerr << " ]\n";
}
#else
#define debug(...) ((void)0)
#define pary(...) ((void)0)
#define safe ((void)0)
#endif // local

using namespace std;

// #include "lib1270.h"
const int maxn = 5005, K = 4;
struct Dsu {
    int pa[maxn], sz[maxn];
    int deg[maxn];
    bool ok;
    // Dsu() { memset(pa, 0, sizeof pa); memset(sz, 0, sizeof sz); memset(deg, 0, sizeof deg); }
    void init(int n) {
        for (int i = 0; i < n; i++) pa[i] = i, sz[i] = 1, deg[i] = 0;
        ok = true;
    }
    int anc(int x) { return x==pa[x] ? x : pa[x]=anc(pa[x]); }
    bool join(int x, int y) {
        if (++deg[x] == 3) ok = false;
        if (++deg[y] == 3) ok = false;
        if ((x=anc(x)) == (y=anc(y))) return ok = false;
        if (sz[x] < sz[y]) swap(x, y);
        return pa[y] = x, sz[x] += sz[y];
    }
} dsu[K], org;
vector<int> cand;
pair<int,int> E[maxn];
vector<int> adj[maxn];
bool inCand[maxn];
int tot, N, cyc;
bool hasFour;
void Init(int _N) {
    N = _N;
    org.init(N);
    for (int i = 0; i < K; i++)
        dsu[i].init(N);
    for (int i = 0; i < N; i++)
        adj[i].clear();
    for (int i = 0; i < N; i++)
        inCand[i] = false;
    cand.clear();
    tot = 0;
    hasFour = false;
    cyc = 0;
}
void addCand(int x) {
    if (cand.size() >= K) return;
    if (inCand[x]) return;
    inCand[x] = true;
    int pos = cand.size();
    cand.push_back(x);
    for (int i = 0; i < tot; i++)
        if (x != E[i].first && x != E[i].second)
            dsu[pos].join(E[i].first, E[i].second);
}
void Link(int a, int b) {
    adj[a].push_back(b);
    adj[b].push_back(a);
    for (int x: {a, b}) {
        if (!hasFour && adj[x].size() == 4) {
            hasFour = true;
            debug("NO", x);
            pary(cand.begin(),cand.end());
            if (inCand[x]) continue;
            inCand[x] = true;
            debug("FOUR", x);
            if (cand.empty()) cand.push_back(7122);
            cand[0] = x;
            dsu[0].init(N);
            for (int i = 0; i < tot; i++)
                if (x != E[i].first && x != E[i].second)
                    dsu[0].join(E[i].first, E[i].second);
        }
    }
    if (adj[a].size() == 3) {
        addCand(a);
        for (int j: adj[a])
            addCand(j);
    }
    if (adj[b].size() == 3) {
        addCand(b);
        for (int j: adj[b])
            addCand(j);
    }
    E[tot++] = { a, b };
    for (int i = 0; i < cand.size(); i++)
        if (a != cand[i] && b != cand[i]) {
            dsu[i].join(a, b);
        }
    if (!org.join(a, b)) {
        debug(cyc, a, b);
        if (cyc)
            cyc = -1;
        else
            cyc = org.sz[org.anc(a)];
    }
}
int CountCritical() {
    if (cand.size() == 0) {
        debug(cyc);
        return cyc ? max(0, cyc) : N;
    }
    pary(cand.begin(),cand.end());
    int ans = 0;
    for (int i = 0; i < N; i++) {
        dsu[0].init(N);
        for (int j = 0; j < tot; j++) if (i != E[j].first && i != E[j].second) dsu[0].join(E[j].first, E[j].second);
        if (dsu[0].ok) ++ans;
    }
    // for (int i = 0; i < cand.size(); i++) if (dsu[i].ok) ++ans;
    return ans;
}
#ifdef local
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    Init(n);
    int q;
    cin >> q;
    while (q--) {
        char c;
        cin >> c;
        if (c == 'L') {
            int x, y;
            cin >> x >> y;
            Link(x, y);
        } else if (c == 'Q') {
            cout << CountCritical() << '\n';
        }
    }
}
#endif // local

/*
6
6
L 1 2
L 2 3
L 4 5
L 4 6
L 1 3
Q
 */

Compilation message

rings.cpp: In function 'void Link(int, int)':
rings.cpp:103:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |     for (int i = 0; i < cand.size(); i++)
      |                     ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 376 ms 844 KB Output is correct
3 Correct 564 ms 964 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 3 ms 904 KB Output is correct
7 Correct 78 ms 752 KB Output is correct
8 Correct 2 ms 844 KB Output is correct
9 Correct 563 ms 844 KB Output is correct
10 Correct 555 ms 928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 1356 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 376 ms 844 KB Output is correct
3 Correct 564 ms 964 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 3 ms 904 KB Output is correct
7 Correct 78 ms 752 KB Output is correct
8 Correct 2 ms 844 KB Output is correct
9 Correct 563 ms 844 KB Output is correct
10 Correct 555 ms 928 KB Output is correct
11 Execution timed out 4083 ms 848 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 376 ms 844 KB Output is correct
3 Correct 564 ms 964 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 3 ms 904 KB Output is correct
7 Correct 78 ms 752 KB Output is correct
8 Correct 2 ms 844 KB Output is correct
9 Correct 563 ms 844 KB Output is correct
10 Correct 555 ms 928 KB Output is correct
11 Execution timed out 4083 ms 848 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 376 ms 844 KB Output is correct
3 Correct 564 ms 964 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 3 ms 904 KB Output is correct
7 Correct 78 ms 752 KB Output is correct
8 Correct 2 ms 844 KB Output is correct
9 Correct 563 ms 844 KB Output is correct
10 Correct 555 ms 928 KB Output is correct
11 Runtime error 2 ms 1356 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -