답안 #758553

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
758553 2023-06-14T21:18:28 Z NK_ 특수한 그래프 (IZhO13_specialg) C++17
0 / 100
3 ms 724 KB
#include <bits/stdc++.h>

using namespace std;

/////////////////////// MACROS ////////////////////////////////////////////
using ll = long long;
using ld = long double;
using db = double;
using str = string;

using pi = pair<int,int>;
using pl = pair<ll,ll>;

using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<str>;
using vc = vector<char>;
using vpi = vector<pi>;
using vpl = vector<pl>;

#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcTU> using PR = pair<T,U>;
tcTU> using umap = unordered_map<T, U>;
tcT> using uset = unordered_set<T>;
tcT> using mset = multiset<T>;

#define mp make_pair
#define f first
#define s second

#define sz(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define ppb pop_back()
#define ppf pop_front()
#define pb push_back
#define eb emplace_back
#define pf push_front

#define lb lower_bound
#define ub upper_bound

// LOOPS
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_, a)
#define each(a,x) for (auto& a: x)

/////////////////////// IMPORANT VARS /////////////////////////////////////

const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INFL = ll(3e18)+10;
const int INF = int(1e9)+10;
const ld PI = acos((ld)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
tcT> using pql = priority_queue<T,vector<T>,less<T>>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define nl '\n'

constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
constexpr int bits(int x) { 
	return x == 0 ? 0 : 31-__builtin_clz(x); }
constexpr int p2(int x) { return 1<<x; }
constexpr int msk2(int x) { return p2(x)-1; }
 
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
 
tcT> bool ckmin(T& a, const T& b) {
	return b < a ? a = b, 1 : 0; } // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }
 
tcTU> T fstTrue(T lo, T hi, U f) {
	hi ++; assert(lo <= hi); // assuming f is increasing
	while (lo < hi) { // find first index such that f is true 
		T mid = lo+(hi-lo)/2;
		f(mid) ? hi = mid : lo = mid+1; 
	} 
	return lo;
}
tcTU> T lstTrue(T lo, T hi, U f) {
	lo --; assert(lo <= hi); // assuming f is decreasing
	while (lo < hi) { // find first index such that f is true 
		T mid = lo+(hi-lo+1)/2;
		f(mid) ? lo = mid : hi = mid-1;
	} 
	return lo;
}
tcT> void remDup(vector<T>& v) { // sort and remove duplicates
	sort(all(v)); v.erase(unique(all(v)),end(v)); }
tcTU> void erase(T& t, const U& u) { // don't erase
	auto it = t.find(u); assert(it != end(t));
	t.erase(it); } // element that doesn't exist from (multi)set


// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;

// tcT> using iset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; 
// #define ook order_of_key
// #define fbo find_by_order

// struct chash { 
// 	const uint64_t C = ll(2e18*PI)+71;
// 	const int RANDOM = rng();
// 	ll operator()(ll x) const {
// 		return __builtin_bswap64((x^RANDOM)*C); }
// };
// template<class K,class V> using um = unordered_map<K,V,chash>;
// template<class K,class V> using ht = gp_hash_table<K,V,chash>;
// template<class K,class V> V get(ht<K,V>& u, K x) {
// 	auto it = u.find(x); return it == end(u) ? 0 : it->s; }

/////////////////////// OUPUT /////////////////////////////////////////////
#define ts to_string
str ts(char c) { return str(1,c); }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
str ts(bool b) {
	#ifdef LOCAL
		return b ? "true" : "false";
	#else
		return ts((int)b);
	#endif
}
tcTU> str ts(pair<T,U> p) {
	#ifdef LOCAL
		return "("+ts(p.f)+", "+ts(p.s)+")";
	#else
		return ts(p.f)+" "+ts(p.s);
	#endif
}

tcTU> str ts(V<pair<T, U>> v) {
	#ifdef LOCAL
		bool fst = 1; str res = "{";
		for (const auto& x: v) {
			if (!fst) res += ", ";
			fst = 0; res += ts(x);
		}
		res += "}"; return res;
	#else
		bool fst = 1; str res = "";
		for (const auto& x: v) {
			if (!fst) res += " ";
			fst = 0; res += ts(x);
		}
		return res;
	#endif
}

tcT> str ts(T v) {
	#ifdef LOCAL
		bool fst = 1; str res = "{";
		for (const auto& x: v) {
			if (!fst) res += ", ";
			fst = 0; res += ts(x);
		}
		res += "}"; return res;
	#else
		bool fst = 1; str res = "";
		for (const auto& x: v) {
			if (!fst) res += " ";
			fst = 0; res += ts(x);
		}
		return res;

	#endif
}

///////////////////////// DEBUG ///////////////////////////////////////////
#define tcTUU tcT, class ...U
void DBG() { cerr << "]" << "\e[0m" << endl; }
tcTUU> void DBG(const T& t, const U&... u) {
	cerr << ts(t); if (sizeof...(u)) cerr << ", ";
	DBG(u...); }
#ifdef LOCAL
	#define dbg(...) cerr << "\e[1m" << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__);
	#define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \
		 << __FUNCTION__  << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
#else
	#define dbg(...) 0
	#define chk(...) 0
#endif	

///////////////////////// FILE I/O ////////////////////////////////////////
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
void setPrec() { cout << fixed << setprecision(15); }
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s = "") {
	unsyncIO(); setPrec();
	#ifndef LOCAL	
		if (sz(s)) setIn(s+".in"), setOut(s+".out"); // for USACO
	#endif
}

///////////////////////// TEMPLATE ABOVE //////////////////////////////////

// REMEMBER
// - Don't Focus On Only One Approach
// - Read And Understand Problem Fully
// - Think Of Edges Cases
// - Implement Carefully
// - Always Check For Overflows
// - Reset Global Variables
// - Look At The Bigger Picture
// - Don't Get Discouraged, You Can Pull It Back

struct TreeTour {
    vi st, en, par, dep;
    V<vi> adj; int t = 0;
    void init(int N) {
        st = en = par = dep = vi(N);
        adj = V<vi>(N);
    }
 	
    void ae(int a, int b) { adj[a].pb(b); }
    void dfs(int x) {
		st[x] = t++;
        each(y,adj[x]) {
            par[y] = x; dep[y] = dep[x]+1; dfs(y); }
        en[x] = t-1;
    	dbg(x, st[x], en[x]);
    }   
    void gen(int R) { par[R] = R; dep[R] = 0; dfs(R); } 
    bool isAnc(int a, int b) { return (st[a] <= st[b] && en[b] <= en[a]); }
};

tcT> struct BIT {
	int N; V<T> data;
	void init(int _N) { N = _N; data = V<T>(N, 0); }
	void add(int p, T x) { for (++p;p<=N;p+=p&-p) data[p-1] ^= x; }
	void add(int l, int r, T x) { add(l, x); add(r+1, x); }
	T get(int r) { T s = 0; for(++r;r;r-=r&-r)s^=data[r-1]; return s; }
};

const ll BIG = 1e15 + 7;

ll get() { return (rng() + MOD) % MOD; }
ll rnd() { return (get() * get()) % BIG; }

void solve() {
	int N; cin >> N;
	vi A(N); each(x, A) { cin >> x; --x; }
	vi deg(N); each(x, A) deg[x]++;

	int Z = count(begin(A), end(A), -1);
	dbg(Z);

	int C = -1; vi cyc;

	vi vis(N);
	F0R(x, N) {
		int u = x;
		vi seen;
		while(!vis[u]) {
			vis[u] = 1; seen.pb(u);
			if (vis[u] == 2 || A[u] == -1) break;
			u = A[u];
		}

		if (vis[u] == 2 || A[u] == -1) {
			each(v, seen) vis[v] = 2;
			continue;
		}

		vis = vi(N, 0);
		while(!vis[u]) {
			vis[u] = 1;
			cyc.pb(u);
			u = A[u];
		}

		dbg(cyc);

		C = cyc[0];
		F0R(i, sz(cyc)) if (deg[A[cyc[i]]] > 1) {
			C = cyc[i];
			rotate(begin(cyc), begin(cyc) + i + 1, end(cyc));
		}

		A[C] = -1;
		break;
	}

	int M = sz(cyc);
	dbg(C, cyc);
	map<int, int> TO;

	BIT<ll> B; B.init(N + M + 5);
	TreeTour T; T.init(N + M + 5);
	int cur = N;

	F0R(u, N) {
		dbg(A[u], u);
		if (A[u] != -1) T.ae(A[u], u);
		
		if (u == C) {
			int prv = C;
			F0R(i, M) {
				T.ae(cur, prv);
				dbg(cur, prv, cyc[i]);
				prv = cur;
				TO[cyc[i]] = cur++;
			}
		}
	}

	F0R(u, N) if (A[u] == -1) {
		int U = u;
		if (u == C) U = TO[U];

		dbg(U);
		T.gen(U);
		B.add(T.st[U], T.en[U], rnd());
	}

	int Q; cin >> Q;
	rep(Q) {
		int t; cin >> t;
		if (t == 1) {
			int u; cin >> u; --u;

			B.add(T.st[u], T.en[u], rnd());
			if (TO.find(u) != TO.end()) {
				B.add(T.st[TO[u]], T.en[TO[u]], rnd());
			}

		} 
		if (t == 2) {
			int u, v; cin >> u >> v; --u, --v;

			if (T.dep[u] < T.dep[v]) {
				if (TO.find(v) != TO.end()) v = TO[v];
				else {
					cout << -1 << nl;
					continue;
				}
			}

			dbg(u, v);
			ll ru = B.get(T.st[u]), rv = B.get(T.st[v]);
			dbg(ru, rv);
			if (ru != rv || !T.isAnc(v, u)) {
				cout << -1 << nl;
				continue;
			} 

			cout << T.dep[u] - T.dep[v] << nl;
		}
	}






}

int main() {
	setIO(); 

	int TT = 1;
	// cin >> TT;

	rep(TT) solve();
	
	exit(0-0);
}

Compilation message

specialg.cpp: In member function 'void TreeTour::dfs(int)':
specialg.cpp:194:19: warning: statement has no effect [-Wunused-value]
  194 |  #define dbg(...) 0
      |                   ^
specialg.cpp:236:6: note: in expansion of macro 'dbg'
  236 |      dbg(x, st[x], en[x]);
      |      ^~~
specialg.cpp: In function 'void solve()':
specialg.cpp:194:19: warning: statement has no effect [-Wunused-value]
  194 |  #define dbg(...) 0
      |                   ^
specialg.cpp:261:2: note: in expansion of macro 'dbg'
  261 |  dbg(Z);
      |  ^~~
specialg.cpp:194:19: warning: statement has no effect [-Wunused-value]
  194 |  #define dbg(...) 0
      |                   ^
specialg.cpp:287:3: note: in expansion of macro 'dbg'
  287 |   dbg(cyc);
      |   ^~~
specialg.cpp:194:19: warning: statement has no effect [-Wunused-value]
  194 |  #define dbg(...) 0
      |                   ^
specialg.cpp:300:2: note: in expansion of macro 'dbg'
  300 |  dbg(C, cyc);
      |  ^~~
specialg.cpp:194:19: warning: statement has no effect [-Wunused-value]
  194 |  #define dbg(...) 0
      |                   ^
specialg.cpp:308:3: note: in expansion of macro 'dbg'
  308 |   dbg(A[u], u);
      |   ^~~
specialg.cpp:194:19: warning: statement has no effect [-Wunused-value]
  194 |  #define dbg(...) 0
      |                   ^
specialg.cpp:315:5: note: in expansion of macro 'dbg'
  315 |     dbg(cur, prv, cyc[i]);
      |     ^~~
specialg.cpp:194:19: warning: statement has no effect [-Wunused-value]
  194 |  #define dbg(...) 0
      |                   ^
specialg.cpp:326:3: note: in expansion of macro 'dbg'
  326 |   dbg(U);
      |   ^~~
specialg.cpp:194:19: warning: statement has no effect [-Wunused-value]
  194 |  #define dbg(...) 0
      |                   ^
specialg.cpp:354:4: note: in expansion of macro 'dbg'
  354 |    dbg(u, v);
      |    ^~~
specialg.cpp:194:19: warning: statement has no effect [-Wunused-value]
  194 |  #define dbg(...) 0
      |                   ^
specialg.cpp:356:4: note: in expansion of macro 'dbg'
  356 |    dbg(ru, rv);
      |    ^~~
specialg.cpp:260:6: warning: unused variable 'Z' [-Wunused-variable]
  260 |  int Z = count(begin(A), end(A), -1);
      |      ^
specialg.cpp: In function 'void setIn(str)':
specialg.cpp:201:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  201 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
specialg.cpp: In function 'void setOut(str)':
specialg.cpp:202:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  202 | void setOut(str s) { freopen(s.c_str(),"w",stdout); }
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 724 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -