답안 #261998

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
261998 2020-08-12T09:02:59 Z lyc Colors (RMI18_colors) C++14
0 / 100
3000 ms 524292 KB
#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
using ii=pair<int,int>;

const int mxN = 150005;

int N, M, A[mxN], B[mxN];
vector<int> al[mxN], atA[mxN], atB[mxN];

struct UFDS {
	vector<int> p, s;
	vector<ii> history;
	
	void init() {
		p.resize(N+1);
		s.resize(N+1);
		FOR(i,1,N){ p[i] = i; s[i] = 1; }
	}
	
	int finds(int i) { return (p[i] == i) ? i : (p[i] = finds(p[i])); }
	
	bool unions(int i, int j) {
		int x = p[i], y = p[j];
		if (x == y) return 0;
		if (s[x] < s[y]) swap(x,y);
		p[y] = x;
		s[x] += s[y];
		history.emplace_back(x,y);
		return 1;
	}
	
	int getTime() { return SZ(history); }
	void undo(int t) {
		while (SZ(history) > t) {
			ii cur = history.back(); history.pop_back();
			int x = cur.first, y = cur.second;
			s[x] -= s[y];
			p[y] = y;
		}
	}
} ufds;

struct Update { int x, y, u, v; };
bool ok;

void dnc(int L, int R, vector<Update>& upd) {
	if (L > R) return;
	
	int t = ufds.getTime();
	vector<Update> ul, ur;
	int m = (L+R)/2;
	for (auto& u : upd) {
		if (L == u.x && R == u.y) ufds.unions(u.u,u.v);
		else if (u.y <= m) ul.push_back(u);
		else if (u.x > m) ur.push_back(u);
		else ul.push_back({u.x, m, u.u, u.v}), ur.push_back({m+1, u.y, u.u, u.v});
	}
	
	if (L == R) {
		set<int> s;
		for (int& x : atA[L]) s.insert(ufds.finds(x));
		for (int& x : atB[L]) ok &= (s.find(ufds.finds(x)) != s.end());
	} else {
		dnc(L,m,ul);
		dnc(m+1,R,ur);
	}
	
	//~ TRACE(L _ R _ t _ ufds.getTime());
	//~ for (auto& u : upd) { if (L == u.x && R == u.y) cout << u.u _ u.v << " :: "; }
	//~ cout << endl;
	
	ufds.undo(t);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    int TC; cin >> TC;
    while (TC--) {
		cin >> N >> M;
		FOR(i,1,N){ cin >> A[i]; }
		FOR(i,1,N){ cin >> B[i]; }
		
		FOR(i,1,N) al[i].clear();
		FOR(i,1,M){
			int U, V; cin >> U >> V;
			al[U].push_back(V);
			al[V].push_back(U);
		}
	
		ok = 1;
		vector<Update> upd;
		FOR(u,1,N){
			if (A[u] < B[u]) { ok = 0; break; }
			for (int& v : al[u]) if (u < v) {
				int x = max(B[u],B[v]), y = min(A[u],A[v]);
				if (x <= y) upd.push_back({ x, y, u, v });
			}
			atA[A[u]].push_back(u);
			atB[B[u]].push_back(u);
		}
		
		if (ok) {
			ufds.init();
			dnc(1,N,upd);
		}
		cout << ok << '\n';
	}
}
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3070 ms 12664 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 338 ms 12132 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3078 ms 12220 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3078 ms 12220 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3070 ms 12664 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1592 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 368 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3070 ms 12664 KB Time limit exceeded
2 Halted 0 ms 0 KB -