QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#600557 | #9424. Stop the Castle 2 | tarjen | RE | 0ms | 3516kb | C++20 | 4.1kb | 2024-09-29 17:21:27 | 2024-09-29 17:21:27 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N=2510,M=2510*10;
class Maxflow{
private:
int nedge=1,p[2*M],nex[2*M],head[N],c[2*M],cur[2*M];
int dist[2*N];
int S,T;
void Addedge(int a,int b,int v){
p[++nedge]=b;nex[nedge]=head[a];head[a]=nedge;
c[nedge]=v;
}
bool bfs(){
queue<int>q;
for(int i=S;i<=T;i++)dist[i]=-1;
dist[S]=0;q.push(S);
while(!q.empty()){
int now=q.front();q.pop();
for(int k=head[now];k;k=nex[k])if(dist[p[k]]==-1&&c[k]>0){
dist[p[k]]=dist[now]+1;
q.push(p[k]);
}
}
return dist[T]>-1;
}
int dfs(int x,int low){
if(x==T)return low;
if(low==0)return 0;
int used=0;
for(int &k=cur[x];k;k=nex[k])if(dist[p[k]]==dist[x]+1&&c[k]>0){
int a=dfs(p[k],min(c[k],low-used));
c[k]-=a;c[k^1]+=a;used+=a;
if(low==used)break;
}
if(used==0)dist[x]=-1;
return used;
}
public:
void init(int s,int t){
for(int i=S;i<=T;i++)head[i]=0;
S=s,T=t;
nedge=1;
}
void addedge(int a,int b,int v){
// cout<<"addedge a="<<a<<" b="<<b<<" v="<<v<<endl;
Addedge(a,b,v);
Addedge(b,a,0);
}
vector<pair<int,int>> dinic(){
int flow=0;
while(bfs()){
for(int i=S;i<=T;i++)cur[i]=head[i];
flow+=dfs(S,1e9);
}
vector<pair<int,int>> v;
for(int i=S+1;i<T;i++){
for(int k=head[i];k;k=nex[k])if(c[k]==0&&(k%2==0)){
v.emplace_back(i,p[k]);
}
}
return v;
}
}sol;
struct point{
int x,y,id,type;
};
void solve()
{
int n,m,k;cin>>n>>m>>k;
k=m-k;
vector<point> a;
for(int i=0;i<n;i++){
int x,y;cin>>x>>y;
a.push_back(point{x,y,i,0});
}
for(int i=0;i<m;i++){
int x,y;cin>>x>>y;
a.push_back(point{x,y,i,1});
}
vector<vector<point>>p;
vector<vector<int>> link(m);
sort(a.begin(),a.end(),[&](point x,point y){
return make_pair(x.x,x.y)<make_pair(y.x,y.y);
});
for(int i=0;i<n+m;i++)if(a[i].type==0){
vector<point> v;
for(int j=i+1;j<n+m;j++){
if(a[j].x!=a[i].x)break;
if(a[j].type==1)v.push_back(a[j]);
else{
for(auto it:v)link[it.id].push_back((int)p.size());
p.push_back(v);
break;
}
}
}
sort(a.begin(),a.end(),[&](point x,point y){
return make_pair(x.y,x.x)<make_pair(y.y,y.x);
});
for(int i=0;i<n+m;i++)if(a[i].type==0){
vector<point> v;
for(int j=i+1;j<n+m;j++){
if(a[j].y!=a[i].y)break;
if(a[j].type==1)v.push_back(a[j]);
else{
for(auto it:v)link[it.id].push_back((int)p.size());
p.push_back(v);
break;
}
}
}
int s=0,t=(int)p.size()+1;
sol.init(s,t);
vector<int> vis(p.size());
map<pair<int,int>,int> edge_id;
for(int i=0;i<m;i++){
assert((int)link[i].size()<=2);
if(link[i].size()==2){
sol.addedge(link[i][0]+1,link[i][1]+1,1);
edge_id[{link[i][0],link[i][1]}]=i;
if(!vis[link[i][0]])vis[link[i][0]]=1,sol.addedge(s,link[i][0]+1,1);
if(!vis[link[i][1]])vis[link[i][1]]=1,sol.addedge(link[i][1]+1,t,1);
}
}
// cout<<"p="<<p.size()<<" edge="<<edge_id.size()<<endl;
auto match=sol.dinic();
int all=p.size();
vector<int>ok(p.size());
vector<int>ans;
vector<int>in(m);
for(auto [x,y]:match)if(k>0){
k--;
x--,y--;
ok[x]=1,ok[y]=1;
all-=2;
in[edge_id[{x,y}]]=1;
}
for(int i=0;i<p.size();i++)if(!ok[i]&&k>0&&!p[i].empty()){
k--;
all--;
ok[i]=1;
in[p[i][0].id]=1;
}
for(int i=0;i<m;i++)if(!in[i]&&k>0){
k--;
in[i]=1;
}
cout<<all<<"\n";
for(int i=0;i<m;i++)if(!in[i])ans.push_back(i+1);
for(auto it:ans)cout<<it<<" ";;cout<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;while(T--)solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3516kb
input:
3 8 6 4 1 3 2 1 2 6 4 1 4 7 6 1 6 3 6 6 2 3 3 1 4 3 4 6 5 2 6 4 3 2 1 10 12 10 10 10 11 1 4 1 5 1 3 2 1 1 2 1 2 2 2 3
output:
4 2 3 5 6 2 2 0 2 3
result:
ok ok 3 cases (3 test cases)
Test #2:
score: -100
Runtime Error
input:
1224 11 17 14 7 3 4 2 8 13 3 15 3 4 5 11 10 2 3 3 8 6 7 11 2 3 10 4 1 3 12 1 2 5 11 9 11 6 11 10 8 15 1 5 9 14 4 11 1 6 10 7 7 6 11 4 8 4 1 11 18 3 2 14 8 2 14 13 13 9 12 14 12 5 6 8 1 10 5 8 6 8 9 6 6 7 5 12 11 6 11 13 5 1 10 7 6 14 5 6 15 2 4 11 1 1 6 4 14 14 13 9 9 3 10 12 7 5 8 13 9 14 1 9 8 4 9...