Submission #210545

# Submission time Handle Problem Language Result Execution time Memory
210545 2020-03-17T16:41:17 Z mahmoudbadawy Port Facility (JOI17_port_facility) C++17
100 / 100
2024 ms 836848 KB
#include <bits/stdc++.h>
#define F first
#define S second

using namespace std;

const int N=1e6+6,MOD=1e9+7;

pair<int,int> arr[N];
int n;
stack<int> st;
int uni[N],sz[N];
deque<int> lst[N];
vector< pair<int,int> > ev,ed;
vector<int> adj[N];
int vis[N],col[N];

int uni_find(int x)
{
	return uni[x]=(uni[x]==x?x:uni_find(uni[x]));
}

void unio(int x,int y)
{
	int a=uni_find(x),b=uni_find(y);
	if(sz[a]>sz[b])
	{
		while(lst[b].size())
		{
			lst[a].push_back(lst[b].front()); lst[b].pop_front();
		}
		uni[b]=a;
		sz[a]+=sz[b];
	}
	else
	{
		while(lst[a].size())
		{
			lst[b].push_front(lst[a].back()); lst[a].pop_back();
		}
		uni[a]=b;
		sz[b]+=sz[a];
	}
}

bool go(int x,int c)
{
	if(vis[x])
		return col[x]==c;
	vis[x]=1; col[x]=c;
	bool ans=1;
	for(int i:adj[x])
		ans&=go(i,c^1);
	return ans;
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		uni[i]=i; sz[i]=1; lst[i].push_back(i);
		scanf("%d %d",&arr[i].F,&arr[i].S);
		ev.push_back({arr[i].F,i});
		ev.push_back({arr[i].S,-i});
	}
	sort(ev.begin(),ev.end());
	for(int i=0;i<ev.size();i++)
	{
		//cout << ev[i].S << endl;
		if(ev[i].S>0)
		{
			int x=uni_find(ev[i].S);
			st.push(x);
		}
		else
		{
			int x=uni_find(-ev[i].S);
			int z=uni_find(st.top()); st.pop();
			if(z==x)
			{
				if(lst[z].back()!=-ev[i].S)
				{
					cout << 0 << endl;
					return 0;
				}
				sz[z]--;
				lst[z].pop_back();
				if(sz[z]) st.push(z);
				continue;
			}
			int y=uni_find(st.top()); st.pop();
			while(y!=x)
			{
				unio(y,z);
				y=uni_find(st.top()); st.pop();
			}
			if(lst[y].back()!=-ev[i].S)
			{
				cout << 0 << endl;
				return 0;
			}
			lst[y].pop_back();
			sz[y]--;
			if(sz[y]) st.push(y);
			st.push(z);
			ed.push_back({y,z});
		}
	}
	for(auto i:ed)
	{
		int x=uni_find(i.F),y=uni_find(i.S);
		//cout << x << " " << y << endl;
		adj[x].push_back(y);
		adj[y].push_back(x);
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		int x=uni_find(i);
		if(!vis[x])
		{
			ans++;
			if(!go(x,0))
			{
				cout << 0 << endl;
				return 0;
			}
		}
	}
	int cur=1;
	while(ans--) cur=(cur*2)%MOD;
	cout << cur << endl;
	return 0;
}

Compilation message

port_facility.cpp: In function 'int main()':
port_facility.cpp:68:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<ev.size();i++)
              ~^~~~~~~~~~
port_facility.cpp:59:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
port_facility.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&arr[i].F,&arr[i].S);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 519 ms 697080 KB Output is correct
2 Correct 502 ms 697080 KB Output is correct
3 Correct 497 ms 697032 KB Output is correct
4 Correct 485 ms 697080 KB Output is correct
5 Correct 492 ms 697080 KB Output is correct
6 Correct 494 ms 697084 KB Output is correct
7 Correct 500 ms 697080 KB Output is correct
8 Correct 502 ms 697080 KB Output is correct
9 Correct 501 ms 697080 KB Output is correct
10 Correct 497 ms 697080 KB Output is correct
11 Correct 501 ms 696952 KB Output is correct
12 Correct 486 ms 696952 KB Output is correct
13 Correct 485 ms 696952 KB Output is correct
14 Correct 493 ms 697080 KB Output is correct
15 Correct 483 ms 697080 KB Output is correct
16 Correct 496 ms 697064 KB Output is correct
17 Correct 494 ms 697080 KB Output is correct
18 Correct 487 ms 696976 KB Output is correct
19 Correct 492 ms 697080 KB Output is correct
20 Correct 492 ms 697080 KB Output is correct
21 Correct 492 ms 697080 KB Output is correct
22 Correct 495 ms 697080 KB Output is correct
23 Correct 492 ms 697024 KB Output is correct
24 Correct 491 ms 697080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 519 ms 697080 KB Output is correct
2 Correct 502 ms 697080 KB Output is correct
3 Correct 497 ms 697032 KB Output is correct
4 Correct 485 ms 697080 KB Output is correct
5 Correct 492 ms 697080 KB Output is correct
6 Correct 494 ms 697084 KB Output is correct
7 Correct 500 ms 697080 KB Output is correct
8 Correct 502 ms 697080 KB Output is correct
9 Correct 501 ms 697080 KB Output is correct
10 Correct 497 ms 697080 KB Output is correct
11 Correct 501 ms 696952 KB Output is correct
12 Correct 486 ms 696952 KB Output is correct
13 Correct 485 ms 696952 KB Output is correct
14 Correct 493 ms 697080 KB Output is correct
15 Correct 483 ms 697080 KB Output is correct
16 Correct 496 ms 697064 KB Output is correct
17 Correct 494 ms 697080 KB Output is correct
18 Correct 487 ms 696976 KB Output is correct
19 Correct 492 ms 697080 KB Output is correct
20 Correct 492 ms 697080 KB Output is correct
21 Correct 492 ms 697080 KB Output is correct
22 Correct 495 ms 697080 KB Output is correct
23 Correct 492 ms 697024 KB Output is correct
24 Correct 491 ms 697080 KB Output is correct
25 Correct 534 ms 697208 KB Output is correct
26 Correct 530 ms 697208 KB Output is correct
27 Correct 497 ms 697208 KB Output is correct
28 Correct 526 ms 697208 KB Output is correct
29 Correct 549 ms 697208 KB Output is correct
30 Correct 564 ms 697208 KB Output is correct
31 Correct 505 ms 697208 KB Output is correct
32 Correct 580 ms 697208 KB Output is correct
33 Correct 546 ms 697080 KB Output is correct
34 Correct 542 ms 697080 KB Output is correct
35 Correct 516 ms 697084 KB Output is correct
36 Correct 536 ms 697208 KB Output is correct
37 Correct 483 ms 697080 KB Output is correct
38 Correct 505 ms 697084 KB Output is correct
39 Correct 492 ms 697080 KB Output is correct
40 Correct 494 ms 697208 KB Output is correct
41 Correct 494 ms 697208 KB Output is correct
42 Correct 492 ms 697164 KB Output is correct
43 Correct 513 ms 697336 KB Output is correct
44 Correct 492 ms 697336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 519 ms 697080 KB Output is correct
2 Correct 502 ms 697080 KB Output is correct
3 Correct 497 ms 697032 KB Output is correct
4 Correct 485 ms 697080 KB Output is correct
5 Correct 492 ms 697080 KB Output is correct
6 Correct 494 ms 697084 KB Output is correct
7 Correct 500 ms 697080 KB Output is correct
8 Correct 502 ms 697080 KB Output is correct
9 Correct 501 ms 697080 KB Output is correct
10 Correct 497 ms 697080 KB Output is correct
11 Correct 501 ms 696952 KB Output is correct
12 Correct 486 ms 696952 KB Output is correct
13 Correct 485 ms 696952 KB Output is correct
14 Correct 493 ms 697080 KB Output is correct
15 Correct 483 ms 697080 KB Output is correct
16 Correct 496 ms 697064 KB Output is correct
17 Correct 494 ms 697080 KB Output is correct
18 Correct 487 ms 696976 KB Output is correct
19 Correct 492 ms 697080 KB Output is correct
20 Correct 492 ms 697080 KB Output is correct
21 Correct 492 ms 697080 KB Output is correct
22 Correct 495 ms 697080 KB Output is correct
23 Correct 492 ms 697024 KB Output is correct
24 Correct 491 ms 697080 KB Output is correct
25 Correct 534 ms 697208 KB Output is correct
26 Correct 530 ms 697208 KB Output is correct
27 Correct 497 ms 697208 KB Output is correct
28 Correct 526 ms 697208 KB Output is correct
29 Correct 549 ms 697208 KB Output is correct
30 Correct 564 ms 697208 KB Output is correct
31 Correct 505 ms 697208 KB Output is correct
32 Correct 580 ms 697208 KB Output is correct
33 Correct 546 ms 697080 KB Output is correct
34 Correct 542 ms 697080 KB Output is correct
35 Correct 516 ms 697084 KB Output is correct
36 Correct 536 ms 697208 KB Output is correct
37 Correct 483 ms 697080 KB Output is correct
38 Correct 505 ms 697084 KB Output is correct
39 Correct 492 ms 697080 KB Output is correct
40 Correct 494 ms 697208 KB Output is correct
41 Correct 494 ms 697208 KB Output is correct
42 Correct 492 ms 697164 KB Output is correct
43 Correct 513 ms 697336 KB Output is correct
44 Correct 492 ms 697336 KB Output is correct
45 Correct 618 ms 704460 KB Output is correct
46 Correct 623 ms 704484 KB Output is correct
47 Correct 611 ms 704504 KB Output is correct
48 Correct 612 ms 704484 KB Output is correct
49 Correct 611 ms 704484 KB Output is correct
50 Correct 605 ms 703588 KB Output is correct
51 Correct 597 ms 702944 KB Output is correct
52 Correct 558 ms 702312 KB Output is correct
53 Correct 609 ms 702612 KB Output is correct
54 Correct 607 ms 701796 KB Output is correct
55 Correct 598 ms 701668 KB Output is correct
56 Correct 596 ms 701644 KB Output is correct
57 Correct 584 ms 705768 KB Output is correct
58 Correct 559 ms 702308 KB Output is correct
59 Correct 601 ms 702820 KB Output is correct
60 Correct 624 ms 703972 KB Output is correct
61 Correct 620 ms 703976 KB Output is correct
62 Correct 587 ms 701540 KB Output is correct
63 Correct 595 ms 702052 KB Output is correct
64 Correct 598 ms 703076 KB Output is correct
65 Correct 591 ms 701684 KB Output is correct
66 Correct 601 ms 701740 KB Output is correct
67 Correct 616 ms 704356 KB Output is correct
68 Correct 632 ms 704524 KB Output is correct
69 Correct 619 ms 703764 KB Output is correct
70 Correct 618 ms 703716 KB Output is correct
71 Correct 617 ms 704484 KB Output is correct
72 Correct 615 ms 704320 KB Output is correct
73 Correct 594 ms 703716 KB Output is correct
74 Correct 599 ms 703832 KB Output is correct
75 Correct 582 ms 708964 KB Output is correct
76 Correct 631 ms 710756 KB Output is correct
77 Correct 653 ms 710836 KB Output is correct
78 Correct 618 ms 704484 KB Output is correct
79 Correct 631 ms 704484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 519 ms 697080 KB Output is correct
2 Correct 502 ms 697080 KB Output is correct
3 Correct 497 ms 697032 KB Output is correct
4 Correct 485 ms 697080 KB Output is correct
5 Correct 492 ms 697080 KB Output is correct
6 Correct 494 ms 697084 KB Output is correct
7 Correct 500 ms 697080 KB Output is correct
8 Correct 502 ms 697080 KB Output is correct
9 Correct 501 ms 697080 KB Output is correct
10 Correct 497 ms 697080 KB Output is correct
11 Correct 501 ms 696952 KB Output is correct
12 Correct 486 ms 696952 KB Output is correct
13 Correct 485 ms 696952 KB Output is correct
14 Correct 493 ms 697080 KB Output is correct
15 Correct 483 ms 697080 KB Output is correct
16 Correct 496 ms 697064 KB Output is correct
17 Correct 494 ms 697080 KB Output is correct
18 Correct 487 ms 696976 KB Output is correct
19 Correct 492 ms 697080 KB Output is correct
20 Correct 492 ms 697080 KB Output is correct
21 Correct 492 ms 697080 KB Output is correct
22 Correct 495 ms 697080 KB Output is correct
23 Correct 492 ms 697024 KB Output is correct
24 Correct 491 ms 697080 KB Output is correct
25 Correct 534 ms 697208 KB Output is correct
26 Correct 530 ms 697208 KB Output is correct
27 Correct 497 ms 697208 KB Output is correct
28 Correct 526 ms 697208 KB Output is correct
29 Correct 549 ms 697208 KB Output is correct
30 Correct 564 ms 697208 KB Output is correct
31 Correct 505 ms 697208 KB Output is correct
32 Correct 580 ms 697208 KB Output is correct
33 Correct 546 ms 697080 KB Output is correct
34 Correct 542 ms 697080 KB Output is correct
35 Correct 516 ms 697084 KB Output is correct
36 Correct 536 ms 697208 KB Output is correct
37 Correct 483 ms 697080 KB Output is correct
38 Correct 505 ms 697084 KB Output is correct
39 Correct 492 ms 697080 KB Output is correct
40 Correct 494 ms 697208 KB Output is correct
41 Correct 494 ms 697208 KB Output is correct
42 Correct 492 ms 697164 KB Output is correct
43 Correct 513 ms 697336 KB Output is correct
44 Correct 492 ms 697336 KB Output is correct
45 Correct 618 ms 704460 KB Output is correct
46 Correct 623 ms 704484 KB Output is correct
47 Correct 611 ms 704504 KB Output is correct
48 Correct 612 ms 704484 KB Output is correct
49 Correct 611 ms 704484 KB Output is correct
50 Correct 605 ms 703588 KB Output is correct
51 Correct 597 ms 702944 KB Output is correct
52 Correct 558 ms 702312 KB Output is correct
53 Correct 609 ms 702612 KB Output is correct
54 Correct 607 ms 701796 KB Output is correct
55 Correct 598 ms 701668 KB Output is correct
56 Correct 596 ms 701644 KB Output is correct
57 Correct 584 ms 705768 KB Output is correct
58 Correct 559 ms 702308 KB Output is correct
59 Correct 601 ms 702820 KB Output is correct
60 Correct 624 ms 703972 KB Output is correct
61 Correct 620 ms 703976 KB Output is correct
62 Correct 587 ms 701540 KB Output is correct
63 Correct 595 ms 702052 KB Output is correct
64 Correct 598 ms 703076 KB Output is correct
65 Correct 591 ms 701684 KB Output is correct
66 Correct 601 ms 701740 KB Output is correct
67 Correct 616 ms 704356 KB Output is correct
68 Correct 632 ms 704524 KB Output is correct
69 Correct 619 ms 703764 KB Output is correct
70 Correct 618 ms 703716 KB Output is correct
71 Correct 617 ms 704484 KB Output is correct
72 Correct 615 ms 704320 KB Output is correct
73 Correct 594 ms 703716 KB Output is correct
74 Correct 599 ms 703832 KB Output is correct
75 Correct 582 ms 708964 KB Output is correct
76 Correct 631 ms 710756 KB Output is correct
77 Correct 653 ms 710836 KB Output is correct
78 Correct 618 ms 704484 KB Output is correct
79 Correct 631 ms 704484 KB Output is correct
80 Correct 1628 ms 772284 KB Output is correct
81 Correct 1625 ms 772436 KB Output is correct
82 Correct 1675 ms 772136 KB Output is correct
83 Correct 1643 ms 772284 KB Output is correct
84 Correct 1676 ms 772284 KB Output is correct
85 Correct 1248 ms 760380 KB Output is correct
86 Correct 1189 ms 758416 KB Output is correct
87 Correct 1241 ms 750780 KB Output is correct
88 Correct 1242 ms 754764 KB Output is correct
89 Correct 1283 ms 747180 KB Output is correct
90 Correct 1206 ms 745020 KB Output is correct
91 Correct 1215 ms 745108 KB Output is correct
92 Correct 1777 ms 785812 KB Output is correct
93 Correct 1217 ms 750780 KB Output is correct
94 Correct 1498 ms 762684 KB Output is correct
95 Correct 1700 ms 768192 KB Output is correct
96 Correct 1670 ms 769760 KB Output is correct
97 Correct 1173 ms 750140 KB Output is correct
98 Correct 1173 ms 754548 KB Output is correct
99 Correct 1234 ms 751716 KB Output is correct
100 Correct 1170 ms 744920 KB Output is correct
101 Correct 1123 ms 744980 KB Output is correct
102 Correct 1658 ms 772444 KB Output is correct
103 Correct 1641 ms 772384 KB Output is correct
104 Correct 1572 ms 765108 KB Output is correct
105 Correct 1541 ms 765104 KB Output is correct
106 Correct 1590 ms 772364 KB Output is correct
107 Correct 1566 ms 772312 KB Output is correct
108 Correct 1502 ms 765220 KB Output is correct
109 Correct 1487 ms 765188 KB Output is correct
110 Correct 2024 ms 835876 KB Output is correct
111 Correct 2001 ms 836848 KB Output is correct
112 Correct 1998 ms 836832 KB Output is correct
113 Correct 1691 ms 772288 KB Output is correct
114 Correct 1676 ms 772284 KB Output is correct