Submission #350031

# Submission time Handle Problem Language Result Execution time Memory
350031 2021-01-18T23:08:10 Z arnold518 Matching (COCI20_matching) C++14
58 / 110
2500 ms 79064 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 2e5;
const int MAXM = 1e6;

int N, M, M1, M2;
pii A[MAXN+10];
vector<int> X[MAXN+10], Y[MAXN+10];
int P[MAXN+10], Q[MAXN+10];
pii E[MAXN+10];

vector<int> adj[MAXM+10], radj[MAXM+10];

bool vis[MAXM+10];
vector<int> S;
void dfs(int now)
{
	vis[now]=true;
	for(int nxt : adj[now])
	{
		if(vis[nxt]) continue;
		dfs(nxt);
	}
	S.push_back(now);
}

int scc[MAXM+10], scnt;
void dfs2(int now)
{
	scc[now]=scnt;
	for(int nxt : radj[now])
	{
		if(scc[nxt]) continue;
		dfs2(nxt);
	}
}

void addEdge(int u, int v)
{
	if(u==0 || v==0) return;
	//printf("%d %d\n", u, v);
	adj[u].push_back(v);
	radj[v].push_back(u);
}

pii nodes[MAXM+10];

int update(int node, int tl, int tr, int p, int k)
{
	if(p<tl || tr<p) return node;
	int ret=++M;
	if(tl==tr)
	{
		addEdge(ret, k);
		return ret;
	}
	int mid=tl+tr>>1;
	nodes[ret]={update(nodes[node].first, tl, mid, p, k), update(nodes[node].second, mid+1, tr, p, k)};
	addEdge(ret, nodes[ret].first);
	addEdge(ret, nodes[ret].second);
	return ret;
}

int main()
{
	scanf("%d", &N);
	for(int i=1; i<=N; i++)
	{
		scanf("%d%d", &A[i].first, &A[i].second);
		X[A[i].first].push_back(i);
		Y[A[i].second].push_back(i);
	}

	for(int i=1; i<=MAXN; i++)
	{
		if(X[i].size()==2)
		{
			M++;
			if(A[X[i][0]]>A[X[i][1]]) swap(X[i][0], X[i][1]);
			E[M]={X[i][0], X[i][1]};
			P[X[i][0]]=M;
			P[X[i][1]]=M;
		}
	}
	M1=M;
	for(int i=1; i<=MAXN; i++)
	{
		if(Y[i].size()==2)
		{
			M++;
			if(A[Y[i][0]]>A[Y[i][1]]) swap(Y[i][0], Y[i][1]);
			E[M]={Y[i][0], Y[i][1]};
			Q[Y[i][0]]=M;
			Q[Y[i][1]]=M;
		}
	}
	M2=M;

	for(int i=1; i<=N; i++)
	{
		if(P[i]==0 && Q[i]==0) 
		{
			return !printf("NE\n");
		}
		if(P[i] && Q[i])
		{
			addEdge(P[i]+M2, Q[i]);
			addEdge(P[i], Q[i]+M2);
			addEdge(Q[i]+M2, P[i]);
			addEdge(Q[i], P[i]+M2);
		}
		else if(P[i])
		{
			addEdge(P[i]+M2, P[i]);
		}
		else if(Q[i])
		{
			addEdge(Q[i]+M2, Q[i]);
		}
	}
	M*=2;


	for(int i=1; i<=M1; i++) for(int j=M1+1; j<=M2; j++)
	{
		if(A[E[i].first].second<=A[E[j].first].second && A[E[j].second].second<=A[E[i].second].second)
		{
			if(A[E[j].first].first<=A[E[i].first].first && A[E[i].second].first<=A[E[j].second].first)
			{
				addEdge(i, j+M2);
				addEdge(j, i+M2);
			}	
		}
	}
/*
	vector<pii> V;
	for(int i=1; i<=M1; i++)
	{
		V.push_back({A[E[i].first].second, i});
		V.push_back({A[E[i].second].second+1, -i});
	}

	for(int i=M1+1; i<=M2; i++)
	{
		V.push_back({A[E[i].first].second, i});
	}
	sort(V.begin(), V.end());

	int root=++M;
	for(auto it : V)
	{
		if(it.second>=M1+1)
		{
			addEdge(it.second, root);
		}
		else
		{
			if(it.second>=0)
			{
				root=update(root, 1, MAXN, A[it.second].first, it.second+M2);
			}
			else
			{
				root=update(root, 1, MAXN, A[-it.second].first, 0);	
			}
		}
	}

	V.clear();
	for(int i=M1+1; i<=M2; i++)
	{
		V.push_back({A[E[i].first].first, i});
		V.push_back({A[E[i].second].first+1, -i});
	}

	for(int i=1; i<=M1; i++)
	{
		V.push_back({A[E[i].first].first, i+M2});
	}
	sort(V.begin(), V.end());

	root=++M;
	for(auto it : V)
	{
		if(it.second>=M2+1)
		{
			addEdge(it.second-M2, root);
		}
		else
		{
			if(it.second>=0)
			{
				root=update(root, 1, MAXN, A[it.second].second, it.second+M2);
			}
			else
			{
				root=update(root, 1, MAXN, A[-it.second].second, 0);	
			}
		}
	}
*/
	for(int i=1; i<=M; i++) if(!vis[i]) dfs(i);
	reverse(S.begin(), S.end());
	for(auto it : S)
	{
		if(scc[it]) continue;
		scnt++;
		dfs2(it);
	}

	for(int i=1; i<=M2; i++)
	{
		//printf("%d %d\n", E[i].first, E[i].second);
		//printf("%d %d\n", scc[i], scc[i+M2]);
		if(scc[i]==scc[i+M2])
		{
			return !printf("NE\n");
		}
	}

	printf("DA\n");
	vector<int> ans;
	for(int i=1; i<=M2; i++)
	{
		if(scc[i]>scc[i+M2])
		{
			printf("%d %d\n", E[i].first, E[i].second);
			ans.push_back(E[i].first);
			ans.push_back(E[i].second);
		}
	}

	assert(ans.size()==N);
	sort(ans.begin(), ans.end());
	ans.erase(unique(ans.begin(), ans.end()), ans.end());
	assert(ans.size()==N);
}

Compilation message

matching.cpp: In function 'int update(int, int, int, int, int)':
matching.cpp:62:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   62 |  int mid=tl+tr>>1;
      |          ~~^~~
In file included from /usr/include/c++/9/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:33,
                 from matching.cpp:1:
matching.cpp: In function 'int main()':
matching.cpp:238:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  238 |  assert(ans.size()==N);
      |         ~~~~~~~~~~^~~
matching.cpp:241:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  241 |  assert(ans.size()==N);
      |         ~~~~~~~~~~^~~
matching.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   71 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
matching.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   74 |   scanf("%d%d", &A[i].first, &A[i].second);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 39 ms 56684 KB Output is correct
2 Correct 39 ms 56684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 56684 KB Output is correct
2 Correct 39 ms 56684 KB Output is correct
3 Correct 39 ms 56684 KB Output is correct
4 Correct 40 ms 56812 KB Output is correct
5 Correct 39 ms 56704 KB Output is correct
6 Correct 39 ms 56684 KB Output is correct
7 Correct 40 ms 56684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 56684 KB Output is correct
2 Correct 39 ms 56684 KB Output is correct
3 Correct 39 ms 56684 KB Output is correct
4 Correct 40 ms 56812 KB Output is correct
5 Correct 39 ms 56704 KB Output is correct
6 Correct 39 ms 56684 KB Output is correct
7 Correct 40 ms 56684 KB Output is correct
8 Correct 42 ms 56704 KB Output is correct
9 Correct 40 ms 56696 KB Output is correct
10 Correct 40 ms 56684 KB Output is correct
11 Correct 39 ms 56684 KB Output is correct
12 Correct 40 ms 56684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 56684 KB Output is correct
2 Correct 39 ms 56684 KB Output is correct
3 Correct 39 ms 56684 KB Output is correct
4 Correct 40 ms 56812 KB Output is correct
5 Correct 39 ms 56704 KB Output is correct
6 Correct 39 ms 56684 KB Output is correct
7 Correct 40 ms 56684 KB Output is correct
8 Correct 42 ms 56704 KB Output is correct
9 Correct 40 ms 56696 KB Output is correct
10 Correct 40 ms 56684 KB Output is correct
11 Correct 39 ms 56684 KB Output is correct
12 Correct 40 ms 56684 KB Output is correct
13 Correct 45 ms 57324 KB Output is correct
14 Correct 45 ms 57344 KB Output is correct
15 Correct 45 ms 57324 KB Output is correct
16 Correct 45 ms 57324 KB Output is correct
17 Correct 44 ms 57324 KB Output is correct
18 Correct 44 ms 57324 KB Output is correct
19 Correct 44 ms 57324 KB Output is correct
20 Correct 44 ms 57324 KB Output is correct
21 Correct 44 ms 57324 KB Output is correct
22 Correct 44 ms 57324 KB Output is correct
23 Correct 45 ms 57580 KB Output is correct
24 Correct 44 ms 57580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 56684 KB Output is correct
2 Correct 39 ms 56684 KB Output is correct
3 Correct 39 ms 56684 KB Output is correct
4 Correct 40 ms 56812 KB Output is correct
5 Correct 39 ms 56704 KB Output is correct
6 Correct 39 ms 56684 KB Output is correct
7 Correct 40 ms 56684 KB Output is correct
8 Correct 42 ms 56704 KB Output is correct
9 Correct 40 ms 56696 KB Output is correct
10 Correct 40 ms 56684 KB Output is correct
11 Correct 39 ms 56684 KB Output is correct
12 Correct 40 ms 56684 KB Output is correct
13 Correct 45 ms 57324 KB Output is correct
14 Correct 45 ms 57344 KB Output is correct
15 Correct 45 ms 57324 KB Output is correct
16 Correct 45 ms 57324 KB Output is correct
17 Correct 44 ms 57324 KB Output is correct
18 Correct 44 ms 57324 KB Output is correct
19 Correct 44 ms 57324 KB Output is correct
20 Correct 44 ms 57324 KB Output is correct
21 Correct 44 ms 57324 KB Output is correct
22 Correct 44 ms 57324 KB Output is correct
23 Correct 45 ms 57580 KB Output is correct
24 Correct 44 ms 57580 KB Output is correct
25 Execution timed out 2591 ms 79064 KB Time limit exceeded
26 Halted 0 ms 0 KB -