【BZOJ 4519】[Cqoi2016] 不同的最小割

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4519

分治+最小割
具体来说,假设随便选两个点来做一次最小割x
那么左右点被分成了两个点集,而任意一对存在于不同点集的点的最小割与x的割相等
于是只用考虑每个点集内部的点,接下来的事就是坚持信仰了

#include<bits/stdc++.h>
#define LL long long
#define abs(x) ((x)>0?(x):-(x))
using namespace std;

const int N = 850+9;
const int M = 8500*2+9;
const int INF = 100000000;

int head[N],nxt[M],to[M],flow[M],cur[N];
int n,m,vout[N*N],cnt,id[N],dis[N],pur,T=1;
queue<int> que;

inline void Add_Edge(int u, int v, int w) {
	to[++T] = v; nxt[T] = head[u]; head[u] = T; flow[T] = w;
	to[++T] = u; nxt[T] = head[v]; head[v] = T; flow[T] = w;
}

inline int read(){
	char c=getchar(); int ret=0,f=1;
	while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
	while (c<='9'&&c>='0') ret=ret*10+c-'0',c=getchar();
	return ret*f;
}

inline bool BFS(int s, int t) {
	memset(dis,-1,sizeof(dis));
	que.push(s); dis[s] = 0;
	
	while (!que.empty()) {
		int w = que.front(); que.pop();
		for (int i=head[w];i;i=nxt[i]) if (flow[i] && !~dis[to[i]]) 
			dis[to[i]] = dis[w] + 1, que.push(to[i]);
	}
	return ~dis[t];
}

int DFS(int w, int f) {
	if (w == pur) return f;
	else {
		int ret = 0;
		for (int &i=cur[w];i;i=nxt[i]) if (flow[i] && dis[to[i]] == dis[w] + 1) {
			int tmp = DFS(to[i], min(f, flow[i]));
			flow[i] -= tmp; flow[i^1] += tmp;
			ret += tmp; f -= tmp; if (!f) return ret;
		}
		return ret;
	}	
}

inline int Dinic(int s, int t){
	int ret = 0; pur = t;
	while (BFS(s,t)) {
		memcpy(cur,head,sizeof(head));
		ret += DFS(s,INF);
	} return ret;
}

void SIGN(int w) {
	dis[w] = 1;
	for (int i=head[w];i;i=nxt[i]) 
		if (!~dis[to[i]] && flow[i]) SIGN(to[i]);
}

void solve(int l , int r){
	if (l >= r) return ;
	static int tmp[N]; int L=l-1, R=r+1;
	
	for (int i=2;i<=T;i+=2) flow[i] = flow[i^1] = flow[i] + flow[i^1] >> 1;
	vout[++cnt] = Dinic(id[l],id[r]);
	
	memset(dis,-1,sizeof(dis)); SIGN(id[l]); 
	for (int i=l;i<=r;i++) 
		if (~dis[id[i]]) tmp[++L] = id[i];
		else tmp[--R] = id[i];
	for (int i=l;i<=r;i++) id[i] = tmp[i];
	solve(l,L); solve(R,r);
}

int main(){
	n = read(); m = read();
	for (int i=1,u,v,w;i<=m;i++) u = read(), v = read(), w = read(), Add_Edge(u,v,w);
	for (int i=1;i<=n;i++) id[i] = i; solve(1,n); 
	int tot = 0; sort(vout+1,vout+1+cnt); 
	for (int i=1;i<=cnt;i++) if (vout[i] != vout[i-1]) tot++;
	printf("%d\n",tot);
	return 0;
}

32 thoughts to “【BZOJ 4519】[Cqoi2016] 不同的最小割”

  1. 314844 911770Sounds like some thing a lot of baby boomers need to study. The feelings of neglect are there in numerous levels when a single is over the hill. 854625

  2. After looking at a handful of the blog posts on your web page, I honestly
    appreciate your technique of writing a blog. I bookmarked it to my bookmark site list and will be checking back in the near future.
    Please visit my website as well and let me know what you think.

  3. hi!,I really like your writing very a lot! proportion we
    keep up a correspondence extra approximately your post on AOL?

    I require a specialist on this area to unravel my problem.
    Maybe that is you! Taking a look forward to look you.

  4. I was suggested this blog by way of my cousin.
    I am no longer positive whether or not this publish is
    written by way of him as no one else recognize such distinct approximately my
    difficulty. You are incredible! Thanks!

  5. 107894 188122I like what you guys are up also. Such intelligent work and reporting! Keep up the superb works guys Ive incorporated you guys to my blogroll. I think itll improve the value of my web site . 345075

  6. 773073 73995Does your website have a contact page? Im having trouble locating it but, Id like to send you an e-mail. Ive got some suggestions for your blog you might be interested in hearing. Either way, excellent blog and I look forward to seeing it develop over time. 584905

  7. 508319 698971Hey, you used to write amazing, but the last several posts have been kinda boring I miss your tremendous writings. Past few posts are just a little bit out of track! come on! 267794

  8. Unquestionably believe that which you stated. Your favorite
    reason seemed to be on the internet the simplest thing to be aware of.
    I say to you, I certainly get irked while people think about
    worries that they just do not know about. You managed to hit the
    nail upon the top and also defined out the whole thing without having side-effects
    , people could take a signal. Will likely be back to get
    more. Thanks

  9. Excellent goods from you, man. I have have in mind your stuff previous to and
    you are simply extremely excellent. I actually like what you’ve acquired here, really like
    what you are saying and the best way during which you are saying it.
    You’re making it entertaining and you continue to care for to keep it sensible.
    I cant wait to read much more from you. This is actually a tremendous web site.

  10. It is appropriate time to make some plans for the future and it is time to be happy.
    I’ve read this post and if I could I wish to
    suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article.
    I desire to read more things about it!

  11. each time i used to read smaller content which also clear their motive, and that is also
    happening with this article which I am reading at this place.

  12. A person necessarily lend a hand to make seriously articles I’d state.
    That is the very first time I frequented your website
    page and up to now? I amazed with the research you made to make this
    actual post amazing. Magnificent activity!

  13. I’ve been surfing online greater than 3 hours these days, yet I
    never discovered any attention-grabbing article like
    yours. It is beautiful value enough for me. In my view, if all site owners
    and bloggers made good content as you probably
    did, the internet will probably be a lot more helpful than ever before.

  14. Hello, Neat post. There is a problem with your site in internet explorer, could check this?
    IE nonetheless is the marketplace leader and a huge element of people will leave out
    your magnificent writing because of this problem.

  15. Hey there! I just wanted to ask if you ever have any trouble with hackers?
    My last blog (wordpress) was hacked and I ended up losing months of hard work due to
    no backup. Do you have any methods to stop hackers?

  16. I’ve been browsing online more than three hours as of late, yet I never discovered any
    fascinating article like yours. It’s pretty value enough for me.

    Personally, if all web owners and bloggers made excellent content material as you
    probably did, the internet will be a lot more useful than ever before.

  17. Hi there just wanted to give you a quick heads up. The text in your content seem to be running off the screen in Internet
    explorer. I’m not sure if this is a formatting issue or something to do with internet browser
    compatibility but I thought I’d post to let you know. The style and design look great though!
    Hope you get the issue fixed soon. Thanks

  18. Yesterday, while I was at work, my cousin stole my iPad and tested to
    see if it can survive a forty foot drop, just so she can be
    a youtube sensation. My iPad is now broken and she has 83
    views. I know this is totally off topic but I had to share it with someone!

  19. Appreciating the hard work you put into your site and detailed information you provide.
    It’s great to come across a blog every once in a while
    that isn’t the same outdated rehashed information.
    Great read! I’ve saved your site and I’m including your
    RSS feeds to my Google account.

  20. Great weblog here! Also your web site quite a bit up very fast!
    What host are you the use of? Can I get your associate link on your host?
    I desire my site loaded up as quickly as yours lol

  21. I blog often and I genuinely appreciate your information. This article has really peaked my interest.
    I am going to bookmark your website and keep checking for new details about once
    a week. I subscribed to your RSS feed too.

Leave a Reply

Your email address will not be published. Required fields are marked *