답안 #430765

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
430765 2021-06-17T03:54:41 Z albertolg101 늑대인간 (IOI18_werewolf) C++17
49 / 100
4000 ms 102720 KB
#include <bits/stdc++.h>
#include "werewolf.h"
	
using namespace std;
using pii = pair<int, int>;
	
vector<int> is_a_graph (int n, vector<vector<int>> &g,
								std::vector<int> S, std::vector<int> E,
								std::vector<int> L, std::vector<int> R) {
	
	int Q = L.size(), m = g.size();
	vector<int> ans;
	
	vector<vector<int>> dp;
	vector<vector<bool>> flag;
	
	function<bool(int, int, int, int, int)> dfs = [&](int nod, bool race, int target, int l, int r)
	{
		if(flag[nod][race])
			return dp[nod][race];
	
		flag[nod][race] = true;
		if(!race and l <= nod and nod <= r)
			dp[nod][race] = dfs(nod, true, target, l, r);
	
		if(nod == target)
			return (dp[nod][race] | race);
	
		for(auto i: g[nod])
		{
			if(!race and l <= i)
				dp[nod][race] |= dfs(i, race, target, l, r); 
			
			if(race and i <= r)
				dp[nod][race] |= dfs(i, race, target, l, r);
		}
	
		return dp[nod][race];
	};
	
	for(int i = 0 ; i < Q ; i++)
	{
		dp = vector<vector<int>> (n, vector<int> (2, 0));
		flag = vector<vector<bool>> (n, vector<bool> (2, false));
		ans.push_back(dfs(S[i], false, E[i], L[i], R[i]));
	}
	
	return ans;
}
	
vector<int> is_a_line (int n, vector<vector<int>> g,
								std::vector<int> S, std::vector<int> E,
								std::vector<int> L, std::vector<int> R) {
	
	int Q = L.size();
	vector<int> cc, rcc(n), ans;
	
	function<void(int, int)> dfs = [&](int nod, int father)
	{
		rcc[nod] = cc.size();
		cc.push_back(nod);

		for(auto i: g[nod])
		{
			if(i != father)
				dfs(i, nod);
		}
	};
	
	for(int i = n - 1 ; i >= 0 ; i--)
	{
		if(g[i].size() == 1)
		{
			dfs(i, -1);
			break;
		}
	}

	vector<vector<int>> rminq(n, vector<int> (18)), rmaxq = rminq;
	
	for(int i = 0 ; i < cc.size() ; i++)
	{
		rminq[i][0] = cc[i];
		rmaxq[i][0] = cc[i];
	}
	
	for(int i = 1 ; (1<<i) < n ; i++)
	{
		for(int j = 0 ; j + (1<<i) - 1 < n ; j++)
		{
			rminq[j][i] = min(rminq[j][i-1], rminq[j+(1<<(i-1))][i-1]);
			rmaxq[j][i] = max(rmaxq[j][i-1], rmaxq[j+(1<<(i-1))][i-1]);
		}
	}
	
	function<pii(int, int)> query = [&](int l, int r)
	{
		int lg = __lg(r-l+1);
		return (pii){
			min(rminq[l][lg], rminq[r-(1<<lg)+1][lg]),
			max(rmaxq[l][lg], rmaxq[r-(1<<lg)+1][lg])
		};
	};

	for(int i = 0 ; i < Q ; i++)
	{
		int s = rcc[S[i]], e = rcc[E[i]], l = L[i], r = R[i];

		bool sol = (e < s);
		if(e < s)
			swap(s, e);
	
		for(int j = 18 ; j >= 0 ; j--)
		{
			int target = s + (1<<j);
	
			if(target > e)
				continue;
	
			if(!sol and l <= query(s, target).first)
				s = target;
			
			if(sol and query(s, target).second <= r)
				s = target;
		}
	
		if(!sol)
			ans.push_back(query(s, e).second <= r);
	
		else
			ans.push_back(l <= query(s, e).first);
	}
	
	return ans ;
}
	
std::vector<int> check_validity(int n, std::vector<int> X, std::vector<int> Y,
								std::vector<int> S, std::vector<int> E,
								std::vector<int> L, std::vector<int> R) {
	
	int Q = L.size(), m = X.size(), maxDegree = 0;
	vector<int> ans;
	vector<vector<int>> g(n);
	
	for(int i = 0 ; i < m ; i++)
	{
		g[X[i]].push_back(Y[i]);
		g[Y[i]].push_back(X[i]);
	
		maxDegree = max({
			maxDegree, 
			(int)g[X[i]].size(), 
			(int)g[Y[i]].size()
		});
	}
	
	if(maxDegree > 2)
		return is_a_graph(n, g, S, E, L, R);
	
	else
		return is_a_line(n, g, S, E, L, R);
}

Compilation message

werewolf.cpp: In function 'std::vector<int> is_a_graph(int, std::vector<std::vector<int> >&, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:11:20: warning: unused variable 'm' [-Wunused-variable]
   11 |  int Q = L.size(), m = g.size();
      |                    ^
werewolf.cpp: In function 'std::vector<int> is_a_line(int, std::vector<std::vector<int> >, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:81:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |  for(int i = 0 ; i < cc.size() ; i++)
      |                  ~~^~~~~~~~~~~
werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:141:6: warning: unused variable 'Q' [-Wunused-variable]
  141 |  int Q = L.size(), m = X.size(), maxDegree = 0;
      |      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 2 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 3 ms 204 KB Output is correct
6 Correct 3 ms 332 KB Output is correct
7 Correct 3 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 2 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 3 ms 204 KB Output is correct
6 Correct 3 ms 332 KB Output is correct
7 Correct 3 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1907 ms 1540 KB Output is correct
11 Correct 1629 ms 1316 KB Output is correct
12 Correct 5 ms 1612 KB Output is correct
13 Correct 1853 ms 1484 KB Output is correct
14 Correct 1539 ms 1356 KB Output is correct
15 Correct 1860 ms 1892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 678 ms 94212 KB Output is correct
2 Correct 892 ms 102632 KB Output is correct
3 Correct 841 ms 102652 KB Output is correct
4 Correct 827 ms 102612 KB Output is correct
5 Correct 779 ms 102720 KB Output is correct
6 Correct 752 ms 102664 KB Output is correct
7 Correct 699 ms 102560 KB Output is correct
8 Correct 700 ms 102564 KB Output is correct
9 Correct 440 ms 102620 KB Output is correct
10 Correct 461 ms 102592 KB Output is correct
11 Correct 473 ms 102652 KB Output is correct
12 Correct 464 ms 102612 KB Output is correct
13 Correct 775 ms 102692 KB Output is correct
14 Correct 743 ms 102620 KB Output is correct
15 Correct 725 ms 102652 KB Output is correct
16 Correct 748 ms 102720 KB Output is correct
17 Correct 699 ms 102648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 2 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 3 ms 204 KB Output is correct
6 Correct 3 ms 332 KB Output is correct
7 Correct 3 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1907 ms 1540 KB Output is correct
11 Correct 1629 ms 1316 KB Output is correct
12 Correct 5 ms 1612 KB Output is correct
13 Correct 1853 ms 1484 KB Output is correct
14 Correct 1539 ms 1356 KB Output is correct
15 Correct 1860 ms 1892 KB Output is correct
16 Correct 678 ms 94212 KB Output is correct
17 Correct 892 ms 102632 KB Output is correct
18 Correct 841 ms 102652 KB Output is correct
19 Correct 827 ms 102612 KB Output is correct
20 Correct 779 ms 102720 KB Output is correct
21 Correct 752 ms 102664 KB Output is correct
22 Correct 699 ms 102560 KB Output is correct
23 Correct 700 ms 102564 KB Output is correct
24 Correct 440 ms 102620 KB Output is correct
25 Correct 461 ms 102592 KB Output is correct
26 Correct 473 ms 102652 KB Output is correct
27 Correct 464 ms 102612 KB Output is correct
28 Correct 775 ms 102692 KB Output is correct
29 Correct 743 ms 102620 KB Output is correct
30 Correct 725 ms 102652 KB Output is correct
31 Correct 748 ms 102720 KB Output is correct
32 Correct 699 ms 102648 KB Output is correct
33 Execution timed out 4074 ms 80972 KB Time limit exceeded
34 Halted 0 ms 0 KB -