103552024-03-31 22:00:34111Úthálózatbővítéscpp17Time limit exceeded 70/100572ms62272 KiB
#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>
using namespace std;

#define ll long long

#define INF (int)1e8

struct P{
	int x,y;
};

struct L{
	P l,h;
};

bool operator==(P a,P b){
	return a.x==b.x&&a.y==b.y;
}

bool operator<(P a,P b){
	return a.x<b.x||a.x==b.x&&a.y<b.y;
}

P operator+(P a,P b){
	return P{a.x+b.x,a.y+b.y};
}

P operator-(P a,P b){
	return P{a.x-b.x,a.y-b.y};
}

istream&operator>>(istream&is,P&p){
	return is>>p.x>>p.y;
}

ostream&operator<<(ostream&os,P p){
	return os<<'('<<p.x<<','<<p.y<<')';
}

ll dot(P a,P b){
	return (ll)a.x*b.x+(ll)a.y*b.y;
}

ll cross(P a,P b){
	return (ll)a.x*b.y-(ll)b.x*a.y;
}

ll sq(ll x){
	return x*x;
}

ll length(L a){
	return sq(a.h.x-a.l.x)+sq(a.h.y-a.l.y);
}

int sign(ll x){
	return (x>0)-(x<0);
}

int orient(P a,P b,P c){
	return sign(cross(a-b,c-b));
}

int right(P a,P b,P c){
	return cross(a-b,c-b)>=0;
}

int test(L a,P b){
	return cross(a.h-a.l,b-a.l)==0&&dot(a.h-a.l,b-a.l)>0&&dot(a.h-a.l,b-a.l)<length(a);
}

int test(L a,L b){
	if(a.l==b.l){
		return test(a,b.h)||test(b,a.h);
	}
	if(a.l==b.h){
		return test(a,b.l)||test(b,a.h);
	}
	if(a.h==b.l){
		return test(a,b.h)||test(b,a.l);
	}
	if(a.h==b.h){
		return test(a,b.l)||test(b,a.l);
	}
	if(orient(a.l,a.h,b.l)==orient(a.l,a.h,b.h)||orient(b.l,b.h,a.l)==orient(b.l,b.h,a.h)){
		return false;
	}
	return true;
}

bool operator<(L a,L b){
	if(a.l==b.h){
		return false;
	}
	if(a.h==b.l){
		return true;
	}
	int x=!right(b.h,b.l,a.l);
	int y=!right(a.h,a.l,b.l);
	if(x==y){
		x=!right(b.h,b.l,a.h);
		y=!right(a.h,a.l,b.h);
	}
	return x>y;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	cin>>N;
	vector<pair<P,int>>v(N+1);
	for(int i=0;i<=N;i++){
		cin>>v[i].first;
		v[i].second=i;
	}
	vector<L>e;
	for(int i=1;i<=N;i++){
		e.push_back(L{v[0].first,v[i].first});
	}
	for(int i=0;i<N-1;i++){
		int a,b;
		cin>>a>>b;
		e.push_back(L{v[a].first,v[b].first});
	}
	for(auto&p:e){
		if(p.h<p.l){
			swap(p.l,p.h);
		}
	}
	vector<int>ans(N,1);
	vector<tuple<P,int,int>>q;
	for(int i=0;i<e.size();i++){
		q.emplace_back(e[i].l,1,i);
		q.emplace_back(e[i].h,2,i);
	}
	sort(q.begin(),q.end());
	set<pair<L,int>>s;
	s.emplace(L{P{-INF,-INF},P{INF,-INF}},-1);
	s.emplace(L{P{-INF,INF},P{INF,INF}},-1);
	for(auto[p,o,i]:q){
		if(o==1){
			auto it=s.insert(pair<L,int>(e[i],i)).first;
			auto lt=prev(it);
			auto rt=next(it);
			while(test(it->first,lt->first)){
				if(it->second<N&&(lt->second>=N||length(it->first)>length(lt->first))){
					ans[it->second]=0;
					it=s.erase(it);
				}
				else{
					ans[lt->second]=0;
					lt=prev(s.erase(lt));
				}
			}
			while(test(it->first,rt->first)){
				if(it->second<N&&(rt->second>=N||length(it->first)>length(rt->first))){
					ans[it->second]=0;
					it=prev(s.erase(it));
				}
				else{
					ans[rt->second]=0;
					rt=s.erase(rt);
				}
			}
		}
		if(o==2){
			auto it=s.find(pair<L,int>(e[i],i));
			if(it!=s.end()){
				it=s.erase(it);
				auto lt=prev(it);
				while(test(it->first,lt->first)){
					if(it->second<N&&(lt->second>=N||length(it->first)>length(lt->first))){
						ans[it->second]=0;
						it=s.erase(it);
					}
					else{
						ans[lt->second]=0;
						lt=prev(s.erase(lt));
					}
				}
			}
		}
	}
	cout<<count(ans.begin(),ans.end(),1)<<'\n';
	for(int i=0;i<N;i++){
		if(ans[i]){
			cout<<i+1<<' ';
		}
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1900 KiB
2Accepted177ms26976 KiB
subtask210/10
3Accepted3ms4100 KiB
4Accepted3ms4332 KiB
5Accepted3ms4468 KiB
6Accepted4ms4712 KiB
7Accepted4ms5108 KiB
subtask320/20
8Accepted3ms5076 KiB
9Accepted3ms5068 KiB
10Accepted4ms5672 KiB
11Accepted14ms7232 KiB
12Accepted28ms8512 KiB
13Accepted144ms20900 KiB
14Accepted314ms39580 KiB
15Accepted29ms16188 KiB
16Accepted64ms20916 KiB
17Accepted111ms27648 KiB
subtask410/10
18Accepted3ms11836 KiB
19Accepted10ms12868 KiB
20Accepted16ms15192 KiB
21Accepted21ms15604 KiB
22Accepted17ms14392 KiB
subtask530/30
23Accepted4ms12944 KiB
24Accepted25ms15628 KiB
25Accepted3ms12904 KiB
26Accepted4ms12948 KiB
27Accepted17ms14824 KiB
28Accepted35ms17052 KiB
29Accepted46ms20008 KiB
30Accepted43ms21712 KiB
31Accepted64ms25592 KiB
32Accepted145ms30384 KiB
subtask60/30
33Accepted108ms34648 KiB
34Accepted71ms31588 KiB
35Accepted307ms49140 KiB
36Accepted298ms48336 KiB
37Accepted298ms48332 KiB
38Accepted409ms58396 KiB
39Accepted421ms58712 KiB
40Time limit exceeded572ms33496 KiB
41Time limit exceeded524ms62272 KiB
42Accepted301ms34112 KiB