社区讨论
最后3个点T飞,求调悬INT_MAX关
P2065[TJOI2011] 卡片参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mjbdj4w0
- 此快照首次捕获于
- 2025/12/18 19:45 2 个月前
- 此快照最后确认于
- 2025/12/20 18:30 2 个月前
CPP
#include<bits/stdc++.h>
#define LoveCyrene ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define gcd(Cyrene,Firefly) __gcd(Cyrene,Firefly)
using namespace std;;
using vp=vector<pair<int,int>>;
constexpr int N=6791;
int Case;
struct Edge{
int destination;
int capacity;
int flow;
int ReverseIndex;
};
vector<Edge> graph[N];
int level[N],CurrentPoint[N];
inline bool haveflow(Edge edge){return edge.capacity>edge.flow;}
void add_edge(int Start,int Destination,int Capacity){
Edge forward={Destination,Capacity,0,(int)graph[Destination].size()};
Edge backward={Start,0,0,(int)graph[Start].size()};
graph[Start].push_back(forward);
graph[Destination].push_back(backward);
}
bool BFS(int source,int sink,int NumNode){
fill(level,level+NumNode+7,-1);
level[source]=0;
queue<int> Queue;
Queue.push(source);
while(Queue.size()){
int Current=Queue.front();Queue.pop();
for (Edge &Next:graph[Current]){
if(level[Next.destination]==-1&&haveflow(Next)){
level[Next.destination]=level[Current]+1;
Queue.push(Next.destination);
if(Next.destination==sink) return 1;
}
}
}
return 0;
}
int DFS(int Current,int sink,int Flow){
if(Current==sink) return Flow;
for (int &i=CurrentPoint[Current];i<graph[Current].size();i++){
Edge &edge=graph[Current][i];
if(level[edge.destination]==level[Current]+1&&haveflow(edge)){
int pushed=DFS(edge.destination,sink,min(Flow,edge.capacity-edge.flow));
if(pushed>0){
edge.flow+=pushed;
graph[edge.destination][edge.ReverseIndex].flow-=pushed;
return pushed;
}
}
}
return 0;
}
int Dinic(int source,int sink,int NumNode){
int maxflow=0;
while(BFS(source,sink,NumNode)){
fill(CurrentPoint,CurrentPoint+NumNode+8,0);
int pushed;
while((pushed=DFS(source,sink,INT_MAX))>0) maxflow+=pushed;
}
return maxflow;
}
int BipartiteGraph(int NumLeft,int NumRight,vp&edges){
int NumNode=NumLeft+NumRight+5;
for (int i=0;i<NumNode;i++)graph[i].clear();
int source=0,sink=NumLeft+NumRight+1;
for (int i=1;i<=NumLeft;i++) add_edge(source,i,1);
for (int i=1;i<=NumRight;i++) add_edge(NumLeft+i,sink,1);
for (auto&edge:edges) add_edge(edge.first,NumLeft+edge.second,1);
return Dinic(source,sink,NumNode);
}
int main(){
vp edges;
LoveCyrene;cin>>Case;
while(Case--){
int NumLeft,NumRight;
cin>>NumRight>>NumLeft;
edges.clear( );
vector<int> Blue(NumRight+2),Red(NumLeft+2);
for (int i=0;i<NumRight;i++)cin>>Blue[i];
for (int i=0;i<NumLeft;i++) cin>>Red[i];
for (int i=0;i<NumLeft;i++){
for (int j=0;j<NumRight;j++){
if(gcd(Red[i],Blue[j])>1){
edges.push_back({i+1,j+1});
}
}
}
cout<<BipartiteGraph(NumLeft,NumRight,edges)<<"\n";
}
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...