模板题
直接 去重
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define mk make_pair
#define ll long long
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int maxn = 1510;
const int maxm = 2e5+1e2;
const int inf = 1e9;
int point[maxn],nxt[maxm],to[maxm];
int cnt=1,n,m;
int x[maxm],y[maxm],w[maxm];
int val[maxm];
int h[maxn];
int s,t;
set<int> se;
void addedge(int x,int y,int w)
{
nxt[++cnt]=point[x];
to[cnt]=y;
val[cnt]=w;
point[x]=cnt;
}
void insert(int x,int y,int w)
{
addedge(x,y,w);
addedge(y,x,0);
}
queue<int> q;
bool bfs(int s)
{
memset(h,-1,sizeof(h));
h[s]=0;
q.push(s);
while (!q.empty())
{
int x = q.front();
q.pop();
for (int i=point[x];i;i=nxt[i])
{
int p = to[i];
if (h[p]==-1 && val[i]>0)
{
h[p]=h[x]+1;
q.push(p);
}
}
}
if (h[t]==-1) return false;
return true;
}
int dfs(int x,int low)
{
if (x==t || low==0) return low;
int totflow=0;
for (int i=point[x];i;i=nxt[i])
{
int p = to[i];
if (h[p]==h[x]+1 && val[i]>0)
{
int tmp = dfs(p,min(low,val[i]));
val[i]-=tmp;
val[i^1]+=tmp;
low-=tmp;
totflow+=tmp;
if (low==0) return totflow;
}
}
if (low>0) h[x]=-1;
return totflow;
}
void init()
{
cnt=1;
memset(point,0,sizeof(point));
}
int dinic(int ss,int tt)
{
int ans=0;
s=ss;
t=tt;
init();
for (int i=1;i<=m;i++) insert(x[i],y[i],w[i]),insert(y[i],x[i],w[i]);
while(bfs(s))
ans=ans+dfs(s,inf);
return ans;
}
int a[maxn];
int col[maxn];
int tot;
int b[maxn],c[maxn];
void find(int x,int wei)
{
col[x]=wei;
for (int i=point[x];i;i=nxt[i])
{
int p = to[i];
if (col[p]!=wei && val[i]>0)
find(p,wei);
}
}
void build(int l,int r)
{
//cout<<l<<" "<<r<<endl;
if (l>=r) return;
int x = a[l],y=a[l+1];
int now = dinic(x,y);
se.insert(now);
++tot;
find(x,tot);
int num=l-1,num1=0,num2=0;
for (int i=l;i<=r;i++) if (col[a[i]]==tot) b[++num1]=a[i];
else c[++num2]=a[i];
for (int i=1;i<=num1;i++) a[++num]=b[i];
for (int i=1;i<=num2;i++) a[++num]=c[i];
build(l,l+num1-1);
build(l+num1,r);
}
int main()
{
n=read();m=read();
for (int i=1;i<=m;i++) x[i]=read(),y[i]=read(),w[i]=read();
for (int i=1;i<=n;i++) a[i]=i;
build(1,n);
cout<<se.size();
return 0;
}