Submission #1059599

# Submission time Handle Problem Language Result Execution time Memory
1059599 2024-08-15T05:57:53 Z pcc Fountain Parks (IOI21_parks) C++17
15 / 100
3500 ms 91956 KB
#include "parks.h"
#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt,sse4")

#define pii pair<int,int>
#define fs first
#define sc second

const int mxn = 2e5+10;
int N;
int S;

struct DSU{
	int dsu[mxn];
	DSU(){
		iota(dsu,dsu+mxn,0);
	}
	int root(int k){
		return k == dsu[k]?k:dsu[k] = root(dsu[k]);
	}
	void onion(int a,int b){
		dsu[root(a)] = root(b);
	}
};

DSU dsu;

struct DINIC{
	struct E{
		int t;
		short f,c;
		E(int tt = 0,int cc = 0,int ff = 0):t(tt),c(cc),f(ff){}
	};
	vector<int> ptr;
	vector<short> lvl;
	vector<vector<int>> paths;
	vector<E> e;
	DINIC(int n = 0){
		lvl = vector<short>(n,0);
		ptr = vector<int>(n,0);
		paths.resize(n);
		e.clear();
	}
	void add_edge(int a,int b,int c,int d = 0){
		paths[a].push_back(e.size());
		e.push_back(E(b,c,0));
		paths[b].push_back(e.size());
		e.push_back(E(a,d,0));
		return;
	}
	queue<int> q;
	bool bfs(int s,int t){
		fill(lvl.begin(),lvl.end(),-1);
		fill(ptr.begin(),ptr.end(),0);
		lvl[s] = 0;
		q.push(s);
		while(!q.empty()){
			auto now = q.front();
			q.pop();
			for(int i = 0;i<paths[now].size();i++){
				int eid = paths[now][i];
				if(e[eid].f == e[eid].c||lvl[e[eid].t] != -1)continue;
				lvl[e[eid].t] = lvl[now]+1;
				q.push(e[eid].t);
			}
		}
		return lvl[t] != -1;
	}
	int dfs(int now,int t,int fl){
		if(now == t)return fl;
		for(int &i = ptr[now];i<paths[now].size();i++){
			int eid = paths[now][i];
			if(e[eid].f == e[eid].c||lvl[e[eid].t] != lvl[now]+1)continue;
			int nxt = e[eid].t;
			int re = 0;
			if(re = dfs(nxt,t,min(fl,e[eid].c-e[eid].f))){
				e[eid].f += re;
				e[eid^1].f -= re;
				return re;
			}
		}
		return 0;
	}
	int flow(int s,int t){
		int ans = 0;
		while(bfs(s,t)){
			int re = 0;
			while(re = dfs(s,t,INT_MAX))ans += re;
		}
		return ans;
	}
};


vector<int> ans[4];

void addans(int s,int e,int x,int y){
	dsu.onion(s,e);
	ans[0].push_back(s);
	ans[1].push_back(e);
	ans[2].push_back(x);
	ans[3].push_back(y);
	return;
}

map<pii,int> mp;
vector<pii> edges;
vector<pii> all;
DINIC g;

void check(int a,int b){
	if(dsu.root(a) == dsu.root(b))return;
	edges.push_back(pii(a,b));
	dsu.onion(a,b);
	return;
}

int construct_roads(std::vector<int> x, std::vector<int> y) {
	S = clock();
    if (x.size() == 1) {
		build({}, {}, {}, {});
        return 1;
    }
	N = x.size();
	for(int i = 0;i<N;i++){
		pii pos = pii(x[i],y[i]);
		if(mp.find(pii(pos.fs-2,pos.sc)) != mp.end())check(mp[pii(pos.fs-2,pos.sc)],i);
		if(mp.find(pii(pos.fs+2,pos.sc)) != mp.end())check(mp[pii(pos.fs+2,pos.sc)],i);
		if(mp.find(pii(pos.fs,pos.sc-2)) != mp.end())check(mp[pii(pos.fs,pos.sc-2)],i);
		if(mp.find(pii(pos.fs,pos.sc+2)) != mp.end())check(mp[pii(pos.fs,pos.sc+2)],i);
		mp[pos] = i;
	}
	if(edges.size() != N-1)return 0;
	for(int i = 0;i<edges.size();i++){
		auto [s,e] = edges[i];
		if(x[s]>x[e])swap(s,e);
		if(x[s] == x[e]&&y[s]>y[e])swap(s,e);
		if(x[s] == x[e]){
			all.push_back(pii(x[s]-1,(y[s]+y[e])>>1));
			all.push_back(pii(x[s]+1,(y[s]+y[e])>>1));
		}
		else if(y[s] == y[e]){
			all.push_back(pii((x[s]+x[e])>>1,y[s]-1));
			all.push_back(pii((x[s]+x[e])>>1,y[s]+1));
		}
	}
	//cerr<<"EDGES: ";for(auto [s,e]:edges)cerr<<s<<' '<<e<<endl;
	sort(all.begin(),all.end());all.erase(unique(all.begin(),all.end()),all.end());
	g = DINIC(all.size()+edges.size()+2);
	int s = all.size()+edges.size();int t = s+1;
	for(int i = 0;i<edges.size();i++){
		g.add_edge(s,i,1);
	}
	for(int i = 0;i<all.size();i++){
		g.add_edge(edges.size()+i,t,1);
	}
	for(int i = 0;i<edges.size();i++){
		auto [s,e] = edges[i];
		if(x[s] == x[e]){
			int p = lower_bound(all.begin(),all.end(),pii(x[s]-1,(y[s]+y[e])>>1))-all.begin();
			g.add_edge(i,p+edges.size(),1);
			p = lower_bound(all.begin(),all.end(),pii(x[s]+1,(y[s]+y[e])>>1))-all.begin();
			g.add_edge(i,p+edges.size(),1);
		}
		else if(y[s] == y[e]){
			int p = lower_bound(all.begin(),all.end(),pii((x[s]+x[e])>>1,y[s]-1))-all.begin();
			g.add_edge(i,p+edges.size(),1);
			p = lower_bound(all.begin(),all.end(),pii((x[s]+x[e])>>1,y[s]+1))-all.begin();
			g.add_edge(i,p+edges.size(),1);
		}
	}
	int re = 0;
	re = g.flow(s,t);
	if(re != N-1)return 0;
	for(int i = 0;i<edges.size();i++){
		for(int j = 0;j<g.paths[i].size();j++){
			int eid = g.paths[i][j];
			int nxt = g.e[eid].t;
			if(g.e[eid].f == 1&&ans[0].size()<N-1){
				addans(edges[i].fs,edges[i].sc,all[nxt-edges.size()].fs,all[nxt-edges.size()].sc);
			}
		}
	}
	build(ans[0],ans[1],ans[2],ans[3]);
	return 1;
}

Compilation message

parks.cpp: In constructor 'DINIC::E::E(int, int, int)':
parks.cpp:35:11: warning: 'DINIC::E::c' will be initialized after [-Wreorder]
   35 |   short f,c;
      |           ^
parks.cpp:35:9: warning:   'short int DINIC::E::f' [-Wreorder]
   35 |   short f,c;
      |         ^
parks.cpp:36:3: warning:   when initialized here [-Wreorder]
   36 |   E(int tt = 0,int cc = 0,int ff = 0):t(tt),c(cc),f(ff){}
      |   ^
parks.cpp: In member function 'bool DINIC::bfs(int, int)':
parks.cpp:64:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |    for(int i = 0;i<paths[now].size();i++){
      |                  ~^~~~~~~~~~~~~~~~~~
parks.cpp: In member function 'int DINIC::dfs(int, int, int)':
parks.cpp:75:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |   for(int &i = ptr[now];i<paths[now].size();i++){
      |                         ~^~~~~~~~~~~~~~~~~~
parks.cpp:80:10: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   80 |    if(re = dfs(nxt,t,min(fl,e[eid].c-e[eid].f))){
      |       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parks.cpp: In member function 'int DINIC::flow(int, int)':
parks.cpp:92:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   92 |    while(re = dfs(s,t,INT_MAX))ans += re;
      |          ~~~^~~~~~~~~~~~~~~~~~
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:137:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  137 |  if(edges.size() != N-1)return 0;
      |     ~~~~~~~~~~~~~^~~~~~
parks.cpp:138: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]
  138 |  for(int i = 0;i<edges.size();i++){
      |                ~^~~~~~~~~~~~~
parks.cpp:155: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]
  155 |  for(int i = 0;i<edges.size();i++){
      |                ~^~~~~~~~~~~~~
parks.cpp:158: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]
  158 |  for(int i = 0;i<all.size();i++){
      |                ~^~~~~~~~~~~
parks.cpp:161: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]
  161 |  for(int i = 0;i<edges.size();i++){
      |                ~^~~~~~~~~~~~~
parks.cpp:179: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]
  179 |  for(int i = 0;i<edges.size();i++){
      |                ~^~~~~~~~~~~~~
parks.cpp:180:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  180 |   for(int j = 0;j<g.paths[i].size();j++){
      |                 ~^~~~~~~~~~~~~~~~~~
parks.cpp:183:37: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  183 |    if(g.e[eid].f == 1&&ans[0].size()<N-1){
      |                        ~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1112 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 0 ms 1116 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 0 ms 1116 KB Output is correct
9 Correct 201 ms 46064 KB Output is correct
10 Correct 13 ms 5968 KB Output is correct
11 Correct 74 ms 26292 KB Output is correct
12 Correct 19 ms 8264 KB Output is correct
13 Correct 24 ms 5332 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 206 ms 46072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1112 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 0 ms 1116 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 0 ms 1116 KB Output is correct
9 Correct 201 ms 46064 KB Output is correct
10 Correct 13 ms 5968 KB Output is correct
11 Correct 74 ms 26292 KB Output is correct
12 Correct 19 ms 8264 KB Output is correct
13 Correct 24 ms 5332 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 206 ms 46072 KB Output is correct
17 Correct 1 ms 1116 KB Output is correct
18 Correct 1 ms 1116 KB Output is correct
19 Correct 0 ms 1116 KB Output is correct
20 Correct 1 ms 1112 KB Output is correct
21 Correct 1 ms 1112 KB Output is correct
22 Correct 0 ms 1116 KB Output is correct
23 Correct 857 ms 75180 KB Output is correct
24 Correct 1 ms 1112 KB Output is correct
25 Correct 3 ms 1628 KB Output is correct
26 Correct 2 ms 1280 KB Output is correct
27 Correct 3 ms 1372 KB Output is correct
28 Correct 224 ms 30788 KB Output is correct
29 Correct 392 ms 44852 KB Output is correct
30 Correct 602 ms 60468 KB Output is correct
31 Correct 908 ms 75008 KB Output is correct
32 Correct 1 ms 1112 KB Output is correct
33 Correct 0 ms 1116 KB Output is correct
34 Correct 0 ms 1116 KB Output is correct
35 Correct 1 ms 1116 KB Output is correct
36 Correct 0 ms 1116 KB Output is correct
37 Correct 0 ms 1116 KB Output is correct
38 Correct 1 ms 1112 KB Output is correct
39 Correct 0 ms 1116 KB Output is correct
40 Correct 1 ms 1228 KB Output is correct
41 Correct 0 ms 1224 KB Output is correct
42 Correct 0 ms 1116 KB Output is correct
43 Correct 2 ms 1372 KB Output is correct
44 Correct 3 ms 1372 KB Output is correct
45 Correct 225 ms 41172 KB Output is correct
46 Correct 384 ms 59312 KB Output is correct
47 Correct 376 ms 59568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1112 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 0 ms 1116 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 0 ms 1116 KB Output is correct
9 Correct 201 ms 46064 KB Output is correct
10 Correct 13 ms 5968 KB Output is correct
11 Correct 74 ms 26292 KB Output is correct
12 Correct 19 ms 8264 KB Output is correct
13 Correct 24 ms 5332 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 206 ms 46072 KB Output is correct
17 Correct 1 ms 1116 KB Output is correct
18 Correct 1 ms 1116 KB Output is correct
19 Correct 0 ms 1116 KB Output is correct
20 Correct 1 ms 1112 KB Output is correct
21 Correct 1 ms 1112 KB Output is correct
22 Correct 0 ms 1116 KB Output is correct
23 Correct 857 ms 75180 KB Output is correct
24 Correct 1 ms 1112 KB Output is correct
25 Correct 3 ms 1628 KB Output is correct
26 Correct 2 ms 1280 KB Output is correct
27 Correct 3 ms 1372 KB Output is correct
28 Correct 224 ms 30788 KB Output is correct
29 Correct 392 ms 44852 KB Output is correct
30 Correct 602 ms 60468 KB Output is correct
31 Correct 908 ms 75008 KB Output is correct
32 Correct 1 ms 1112 KB Output is correct
33 Correct 0 ms 1116 KB Output is correct
34 Correct 0 ms 1116 KB Output is correct
35 Correct 1 ms 1116 KB Output is correct
36 Correct 0 ms 1116 KB Output is correct
37 Correct 0 ms 1116 KB Output is correct
38 Correct 1 ms 1112 KB Output is correct
39 Correct 0 ms 1116 KB Output is correct
40 Correct 1 ms 1228 KB Output is correct
41 Correct 0 ms 1224 KB Output is correct
42 Correct 0 ms 1116 KB Output is correct
43 Correct 2 ms 1372 KB Output is correct
44 Correct 3 ms 1372 KB Output is correct
45 Correct 225 ms 41172 KB Output is correct
46 Correct 384 ms 59312 KB Output is correct
47 Correct 376 ms 59568 KB Output is correct
48 Correct 1 ms 1116 KB Output is correct
49 Correct 0 ms 1116 KB Output is correct
50 Correct 0 ms 1116 KB Output is correct
51 Correct 0 ms 1116 KB Output is correct
52 Correct 1 ms 1116 KB Output is correct
53 Correct 1 ms 1116 KB Output is correct
54 Correct 1 ms 1116 KB Output is correct
55 Incorrect 1162 ms 64392 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1112 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 0 ms 1116 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 0 ms 1116 KB Output is correct
9 Correct 201 ms 46064 KB Output is correct
10 Correct 13 ms 5968 KB Output is correct
11 Correct 74 ms 26292 KB Output is correct
12 Correct 19 ms 8264 KB Output is correct
13 Correct 24 ms 5332 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 206 ms 46072 KB Output is correct
17 Correct 1 ms 1112 KB Output is correct
18 Correct 1 ms 1224 KB Output is correct
19 Correct 1 ms 1112 KB Output is correct
20 Execution timed out 3585 ms 65344 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1112 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 0 ms 1116 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 0 ms 1116 KB Output is correct
9 Correct 201 ms 46064 KB Output is correct
10 Correct 13 ms 5968 KB Output is correct
11 Correct 74 ms 26292 KB Output is correct
12 Correct 19 ms 8264 KB Output is correct
13 Correct 24 ms 5332 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 206 ms 46072 KB Output is correct
17 Correct 502 ms 90876 KB Output is correct
18 Correct 549 ms 91956 KB Output is correct
19 Execution timed out 3535 ms 65076 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1112 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 0 ms 1116 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 0 ms 1116 KB Output is correct
9 Correct 201 ms 46064 KB Output is correct
10 Correct 13 ms 5968 KB Output is correct
11 Correct 74 ms 26292 KB Output is correct
12 Correct 19 ms 8264 KB Output is correct
13 Correct 24 ms 5332 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 206 ms 46072 KB Output is correct
17 Correct 1 ms 1116 KB Output is correct
18 Correct 1 ms 1116 KB Output is correct
19 Correct 0 ms 1116 KB Output is correct
20 Correct 1 ms 1112 KB Output is correct
21 Correct 1 ms 1112 KB Output is correct
22 Correct 0 ms 1116 KB Output is correct
23 Correct 857 ms 75180 KB Output is correct
24 Correct 1 ms 1112 KB Output is correct
25 Correct 3 ms 1628 KB Output is correct
26 Correct 2 ms 1280 KB Output is correct
27 Correct 3 ms 1372 KB Output is correct
28 Correct 224 ms 30788 KB Output is correct
29 Correct 392 ms 44852 KB Output is correct
30 Correct 602 ms 60468 KB Output is correct
31 Correct 908 ms 75008 KB Output is correct
32 Correct 1 ms 1112 KB Output is correct
33 Correct 0 ms 1116 KB Output is correct
34 Correct 0 ms 1116 KB Output is correct
35 Correct 1 ms 1116 KB Output is correct
36 Correct 0 ms 1116 KB Output is correct
37 Correct 0 ms 1116 KB Output is correct
38 Correct 1 ms 1112 KB Output is correct
39 Correct 0 ms 1116 KB Output is correct
40 Correct 1 ms 1228 KB Output is correct
41 Correct 0 ms 1224 KB Output is correct
42 Correct 0 ms 1116 KB Output is correct
43 Correct 2 ms 1372 KB Output is correct
44 Correct 3 ms 1372 KB Output is correct
45 Correct 225 ms 41172 KB Output is correct
46 Correct 384 ms 59312 KB Output is correct
47 Correct 376 ms 59568 KB Output is correct
48 Correct 1 ms 1116 KB Output is correct
49 Correct 0 ms 1116 KB Output is correct
50 Correct 0 ms 1116 KB Output is correct
51 Correct 0 ms 1116 KB Output is correct
52 Correct 1 ms 1116 KB Output is correct
53 Correct 1 ms 1116 KB Output is correct
54 Correct 1 ms 1116 KB Output is correct
55 Incorrect 1162 ms 64392 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -