Submission #716422

# Submission time Handle Problem Language Result Execution time Memory
716422 2023-03-30T05:27:38 Z parsadox2 Love Polygon (BOI18_polygon) C++17
0 / 100
241 ms 30136 KB
#include <bits/stdc++.h>
#define pb 		push_back
#define F		first
#define S 		second
#define debug(x)    cout << #x << "= " << x << ", "
#define ll 		long long
#define fast 		ios::sync_with_stdio(false), cin.tie(0),  cout.tie(0)
#define SZ(x)         (int) x.size()
#define wall 		cout << endl;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 1e5 + 10;
int n , ar[maxn] , dp[maxn][2] , super_man;
map <string , int> mp;
vector <pair<string  , string>> edges;
vector <int> adj[maxn] , trav;
bool vis[maxn] , marked[maxn];

int Find_root(int v)
{
	vis[v] = true;
	if(vis[ar[v]])  return v;
	return Find_root(ar[v]);
}

void dfs(int v)
{
	marked[v] = true;
	bool flg = false;
	for(auto u : adj[v])  if(!marked[u])
	{
		dfs(u);
		dp[v][0] += dp[u][1];
		dp[v][1] += dp[u][1];
		if(dp[u][1] == dp[u][0])  flg = true;
	}
	dp[v][1] += flg;
}

void dfs2(int v , int root)
{
	bool flg = false;
	dp[v][0] = dp[v][1] = 0;
	for(auto u : adj[v])  if(u != root)
	{
		dfs2(u , root);
		dp[v][0] += dp[u][1];
		dp[v][1] += dp[u][1];
		if(dp[u][1] == dp[u][0])  flg = true;
	}
	dp[v][1] += flg;
	if(v == super_man)  dp[v][1] = dp[v][0];
}

int solve(int root)
{
	dfs(root);
	int res = dp[root][1];
	if(ar[root] == root)  return res;
	super_man = ar[root];
	dfs2(root , root);
	int tmp = (ar[ar[root]] == root ? 2 : 1);
	res = max(res , dp[root][0] + tmp);
	return res;
}

int32_t main()
{
	fast;
	cin >> n;
	for(int i = 0 ; i < n ; i++)
	{
		string a , b;  cin >> a >> b;
		edges.pb({a , b});
		mp[a] = i;
	}
	for(auto u : edges)
	{
		int s = mp[u.F] , e = mp[u.S];
		ar[s] = e;
		adj[e].pb(s);
	}
	int ans = n;
	for(int i = 0 ; i < n ; i++)   if(!marked[i])
	{
		int r = Find_root(i);
		ans -= solve(r);
	}
	cout << ans << endl;
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Incorrect 2 ms 2644 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2680 KB Output is correct
3 Correct 2 ms 2680 KB Output is correct
4 Incorrect 237 ms 30136 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 208 ms 22552 KB Output is correct
2 Correct 241 ms 24208 KB Output is correct
3 Correct 171 ms 23368 KB Output is correct
4 Incorrect 201 ms 21844 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Incorrect 2 ms 2644 KB Output isn't correct
6 Halted 0 ms 0 KB -