Submission #781864

# Submission time Handle Problem Language Result Execution time Memory
781864 2023-07-13T12:07:56 Z Charizard2021 Potemkin cycle (CEOI15_indcyc) C++17
100 / 100
235 ms 173008 KB
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);; cin.tie(NULL)
void selfMax(long long& a, long long b){
    a = max(a, b);
}
void selfMin(long long& a, long long b){
    a = min(a, b);
}
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& x) {return in >> x.first >> x.second;}

template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& x) {return out << x.first << ' ' << x.second;}
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {for(auto &x : a) out << x << ' '; return out;};
template<typename T> ostream& operator<<(ostream& out, vector<vector<T> >& a) {for(auto &x : a) out << x << '\n'; return out;};
template<typename T1, typename T2> ostream& operator<<(ostream& out, vector<pair<T1, T2> >& a) {for(auto &x : a) out << x << '\n'; return out;};

/*

Use DSU dsu(N);

*/
struct DSU {
	vector<long long> adj2;
	DSU(long long N) { adj2 = vector<long long>(N, -1); }

	// get representive component (uses path compression)
	long long get(long long x) { return adj2[x] < 0 ? x : adj2[x] = get(adj2[x]); }

	bool same_set(long long a, long long b) { return get(a) == get(b); }

	long long size(long long x) { return -adj2[get(x)]; }

	bool unite(long long x, long long y) {  // union by size
		x = get(x), y = get(y);
		if (x == y) return false;
		if (adj2[x] > adj2[y]) swap(x, y);
		adj2[x] += adj2[y]; adj2[y] = x;
		return true;
	}
};
/*

Run with Bit<long long> BIT

*/
template <class T> class BIT {
    private:
        long long size;
        vector<T> bit;
        vector<T> arr;
    public:
        BIT(long long size) : size(size), bit(size + 1), arr(size) {}
        void set(long long ind, long long val) { add(ind, val - arr[ind]); }
        void add(long long ind, long long val) {
            arr[ind] += val;
            ind++;
            for (; ind <= size; ind += ind & -ind) { bit[ind] += val; }
        }
        T pref_sum(long long ind) {
            ind++;
            T total = 0;
            for (; ind > 0; ind -= ind & -ind) { total += bit[ind]; }
            return total;
        }
};
/*

Change Comb for specific Seg tree probs, but run with Seg<long long> Seg;

*/
template<class T> struct Seg {
	const T ID = 1e18; T comb(T a, T b) { return min(a,b); }
	long long n; vector<T> seg;
	void init(long long _n) { n = _n; seg.assign(2*n,ID); }
	void pull(long long p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
	void upd(long long p, T val) {
		seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
	T query(long long l, long long r) {
		T ra = ID, rb = ID;
		for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
			if (l&1) ra = comb(ra,seg[l++]);
			if (r&1) rb = comb(seg[--r],rb);
		}
		return comb(ra,rb);
	}
};
const int N = 1000, M = 100000;


int a[M], b[M], c[2][M];

bool adj2[N][N];
vector<pair<int, int>> adj[N];
vector<pair<int, int>> adj3[2][M];
vector<int> st;
int visited[2][M];
void dfs(int t, int u, pair<int, int> p) {
    visited[t][u] = 1;
    st.emplace_back(c[t][u]);
    for (auto v: adj3[t][u]) {
        if (v != p) {
            if (visited[v.first][v.second] == 1) {
                cout << c[v.first][v.second] + 1 << " ";
                int cur = c[v.first][v.second];
                int val = 0;
                while (true) {
                    auto u = st.back();
                    cout << u + 1 << " ";
                    if (val && adj2[u][cur]) {
                        break;
                    }
                    val++;
                    st.pop_back();
                }
                exit(0);
            }
        }
    }
    for (auto v : adj3[t][u]) {
        if (v != p) {
            if (!visited[v.first][v.second]) {
                dfs(v.first, v.second, {t, u});
            }
        }
    }
    visited[t][u] = 2;
    st.pop_back();
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, r;
    cin >> n >> r;
    for (int i = 0; i < r; ++i) {
        cin >> a[i] >> b[i];
        --a[i], --b[i];
        c[0][i] = a[i];
        c[1][i] = b[i];
        adj[a[i]].emplace_back(b[i], i);
        adj[b[i]].emplace_back(a[i], i);
        adj2[a[i]][b[i]] = adj2[b[i]][a[i]] = true;
    }
    for (int u = 0; u < n; ++u) {
        for (int i = 0; i < (int)adj[u].size(); i++) {
            for(int j = 0; j < (int)adj[u].size(); j++){
                if (i != j && !adj2[adj[u][i].first][adj[u][j].first]) {
                    int t1 = (b[adj[u][i].second] == adj[u][i].first);
                    int t2 = (b[adj[u][j].second] == u);
                    adj3[t1][adj[u][i].second].emplace_back(t2, adj[u][j].second);
                }
            }
        }
    }
    for (int u = 0; u < r; ++u) {
        if (!visited[0][u]) {
            dfs(0, u, {-1, -1});
        }
        if (!visited[1][u]) {
            dfs(1, u, {-1, -1});
        }
    }
    cout << "no";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5008 KB Output is correct
3 Correct 2 ms 5052 KB Output is correct
4 Correct 2 ms 5052 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5588 KB Output is correct
2 Correct 5 ms 5696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5844 KB Output is correct
2 Correct 4 ms 5844 KB Output is correct
3 Correct 9 ms 9684 KB Output is correct
4 Correct 15 ms 9756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8272 KB Output is correct
2 Correct 7 ms 8532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 36172 KB Output is correct
2 Correct 22 ms 12220 KB Output is correct
3 Correct 98 ms 36820 KB Output is correct
4 Correct 22 ms 12300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 95128 KB Output is correct
2 Correct 118 ms 103536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 9692 KB Output is correct
2 Correct 112 ms 10332 KB Output is correct
3 Correct 201 ms 172804 KB Output is correct
4 Correct 235 ms 173008 KB Output is correct