Submission #846282

# Submission time Handle Problem Language Result Execution time Memory
846282 2023-09-07T13:11:51 Z vjudge1 KOVANICE (COI15_kovanice) C++17
100 / 100
280 ms 38768 KB
#include <bits/stdc++.h> 
#define pb push_back
using namespace std;


constexpr static int MXN = 3e5 + 5;

struct Node 
{
	int head, tail, size, nxt; 
	Node() {} 
	void init(int i) { head = i, tail = i, size = 1, nxt = -1; } 
};

Node node[MXN]; 
vector<int> g[MXN]; 
vector<int> rg[MXN];
bitset<MXN> visited;
int res[MXN];
int ucount[MXN];
int dcount[MXN];

void dfs1(int node)
{
	visited[node] = true;
	dcount[node] = 0;
	for (int c : g[node])
	{
		if (!visited[c])
			dfs1(c);
		dcount[node] = max(dcount[node], dcount[c] + 1);
	}
}

void dfs2(int node)
{
	visited[node] = true;
	ucount[node] = 0;
	for (int c : rg[node])
	{
		if (!visited[c])
			dfs2(c);
		ucount[node] = max(ucount[node], ucount[c] + 1);
	}
}

void merge(int a, int b);

int n, m;


int main()
{
	int v;
	cin >> n >> m >> v;
	vector<array<int, 2>> edges;
	for (int i = 0; i < m; i++)
		node[i].init(i);
	while (v--)
	{
		int a, b;
		char c;
		scanf("%i%c%i", &a, &c, &b);
		a--,b--;
		if (c == '=' && node[a].head != node[b].head)
			merge(node[a].head, node[b].head);
		else if (c == '<')
			edges.pb({b, a});
	}
	for (auto [a, b] : edges)
	{
		g[node[a].head].pb(node[b].head);
		rg[node[b].head].pb(node[a].head);
	}
	for (int i = 0; i < m; i++)
		if (i == node[i].head && !visited[i])
			dfs1(i);
	visited.reset();

	for (int i = 0; i < m; i++)
		if (i == node[i].head && !visited[i])
			dfs2(i);
	for (int i = 0; i < m; i++)
		if (i == node[i].head)
			for (int j = i; j != -1; j = node[j].nxt)
				res[j] = (ucount[i] + dcount[i] == n-1) ? (dcount[i] + 1) : -1;
	for (int i = 0; i < m; i++)
	{
		assert(res[i]);
		if (res[i] != -1)
			cout << "K" << res[i] << "\n";
		else
			cout << "?\n";
	}
}


void merge(int a, int b)
{
	if (node[b].size > node[a].size) swap(a, b);
	node[a].size += node[b].size;
	node[node[a].tail].nxt = b;
	node[a].tail = node[b].tail;
	for (;b != -1; b = node[b].nxt)
		node[b].head = a;
}

Compilation message

kovanice.cpp: In function 'int main()':
kovanice.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |   scanf("%i%c%i", &a, &c, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 18776 KB Output is correct
2 Correct 4 ms 18776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 24520 KB Output is correct
2 Correct 70 ms 24864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 20688 KB Output is correct
2 Correct 20 ms 20688 KB Output is correct
3 Correct 20 ms 20688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 33176 KB Output is correct
2 Correct 197 ms 32416 KB Output is correct
3 Correct 194 ms 32064 KB Output is correct
4 Correct 258 ms 37848 KB Output is correct
5 Correct 280 ms 38768 KB Output is correct