Submission #383410

#TimeUsernameProblemLanguageResultExecution timeMemory
383410luciocfBitaro’s Party (JOI18_bitaro)C++14
0 / 100
18 ms18540 KiB
#include <bits/stdc++.h>

#define ff first
#define ss second

using namespace std;

typedef pair<int, int> pii;

const int maxn = 2e5+10;
const int inf = 1e9+10;

int n, m, q;
int dp[maxn];

bool mark[maxn], block[maxn];

int ptr[maxn];

vector<pii> dist[maxn];

vector<int> grafo[2][maxn], ord;

void dfs(int u)
{
	mark[u] = 1;

	for (auto v: grafo[0][u])
		if (!mark[v])
			dfs(v);

	ord.push_back(u);
}

void solve(int t)
{
	for (int i = 1; i <= n; i++)
		dp[i] = -inf, mark[i] = 0;

	dp[t] = 0;
	
	for (int i = ord.size()-1; i >= 0; i--)
	{
		int u = ord[i];
		for (auto v: grafo[1][u])
			dp[u] = max(dp[u], 1 + dp[v]);
	}

}

int main(void)
{
	scanf("%d %d %d", &n, &m, &q);

	for (int i = 1; i <= m; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);

		grafo[0][v].push_back(u);
		grafo[1][u].push_back(v);
	}

	for (int i = 1; i <= n; i++)
		if (!mark[i])
			dfs(i);

	for (auto u: ord)
	{
		int s = 1;

		for (auto v: grafo[0][u])
		{
			ptr[v] = 0;
			s += dist[v].size();
		}

		s = min(s, 321);

		for (int t = 1; t <= s; t++)
		{	
			pii pp = {-1, -1};

			for (auto v: grafo[0][u])
				if (ptr[v] < dist[v].size())
					pp = max(pp, {dist[v][ptr[v]].ff+1, dist[v][ptr[v]].ss});

			if (pp.ff == -1) dist[u].push_back({0, u});
			else
			{
				dist[u].push_back(pp);

				for (auto v: grafo[0][u])
				{
					if (ptr[v] < dist[v].size() && pp == make_pair(dist[v][ptr[v]].ff+1, dist[v][ptr[v]].ss))
					{
						ptr[v]++;
						break;
					}
				}
			}
		}
	}

	while (q--)
	{
		int t, y;
		scanf("%d %d", &t, &y);

		vector<int> lista;

		for (int i = 1; i <= y; i++)
		{
			int u;
			scanf("%d", &u);

			lista.push_back(u);
			block[u] = 1;
		}

		int ans = -inf;

		if (y > 320)
		{
			solve(t);

			for (int i = 1; i <= n; i++)
				if (!block[i])
					ans = max(ans, dp[i]);
		}
		else
		{
			for (auto pp: dist[t])
			{
				if (!block[pp.ss])
				{
					ans = pp.ff;
					break;
				}
			}
		}

		if (ans == -inf) printf("-1\n");
		else printf("%d\n", ans);

		for (auto u: lista)
			block[u] = 0;
	}
}

Compilation message (stderr)

bitaro.cpp: In function 'int main()':
bitaro.cpp:85:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |     if (ptr[v] < dist[v].size())
      |         ~~~~~~~^~~~~~~~~~~~~~~~
bitaro.cpp:95:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |      if (ptr[v] < dist[v].size() && pp == make_pair(dist[v][ptr[v]].ff+1, dist[v][ptr[v]].ss))
      |          ~~~~~~~^~~~~~~~~~~~~~~~
bitaro.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   53 |  scanf("%d %d %d", &n, &m, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   58 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
bitaro.cpp:108:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  108 |   scanf("%d %d", &t, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
bitaro.cpp:115:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  115 |    scanf("%d", &u);
      |    ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...