【BZOJ 2438】[中山市选2011] 杀人游戏

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2438
数据生成器:http://paste.ubuntu.com/19584806/
PS:此题的随机数据不容易拍出错QAQ

来练习Tarjan的scc,然而此题细节比较多,被卡了好久QAQ

很明显,如果一个scc中知道了一个点,则其他点都可以安全推出
所以我们只需要搞出入度为零的scc有多少个即可

然而还有一个坑:
如果最后问得只剩一个单独的点了,这个点是不需要询问的
所以遇到这种单独的点,并且他连向的点的入度都不为一的话,ans -= 1

#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
using namespace std;

const int MAXN = 300000+9;

int n,m,to[MAXN],nxt[MAXN],head[MAXN];
int num[MAXN],lowlink[MAXN],in[MAXN],vout;
int scc_num[MAXN],scc_sz[MAXN],scc_cnt;
stack<int> sta;

inline void AddEdge(int u, int v){
	static int T = 0; 
	to[++T] = v; nxt[T] = head[u]; head[u] = T;
}

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

void DFS(int w) {
	static int tot = 0; sta.push(w);
	num[w] = lowlink[w] = ++tot;
	for (int i=head[w];i;i=nxt[i]) {
		if (!num[to[i]]) DFS(to[i]), lowlink[w] = min(lowlink[w], lowlink[to[i]]);
		else if (!scc_num[to[i]]) lowlink[w] = min(lowlink[w], lowlink[to[i]]);
	}
	if (lowlink[w] == num[w]) {
		scc_cnt += 1;
		while (true) {
			int nw = sta.top(); sta.pop();
			scc_num[nw] = scc_cnt;
			scc_sz[scc_cnt]++;
			if (nw == w) break;
		}
	}
}

int main(){
	n = read(); m = read(); if (n==1) {printf("1.000000\n"); return 0;}
	for (int i=1,u,v;i<=m;i++) u = read(), v = read(), AddEdge(u,v); 
	for (int i=1;i<=n;i++) if (!num[i]) DFS(i);
	for (int i=1;i<=n;i++) for (int j=head[i];j;j=nxt[j])
		if (scc_num[to[j]] != scc_num[i]) in[scc_num[to[j]]]++;
	for (int i=1;i<=scc_cnt;i++) if (!in[i]) vout++; 
	for (int i=1,tag=0;i<=n;i++,tag=0) if (scc_sz[scc_num[i]] == 1 && !in[scc_num[i]]) {
		for (int j=head[i];j;j=nxt[j]) if (in[scc_num[to[j]]] == 1 && scc_num[to[j]] != scc_num[i]) {tag = 1; break;}
		if (!tag) {vout--; break;}
	}
	printf("%.6lf\n",1.0-(double)vout/n);
	return 0;
} 

为了学习仙人掌才来看的这个,然而现在发现仙人掌用的是无向图的TarjanQAQ

31 thoughts to “【BZOJ 2438】[中山市选2011] 杀人游戏”

  1. hello!,I really like your writing very a lot! percentage we keep in touch more approximately your post on AOL?
    I require a specialist on this space to resolve my
    problem. May be that is you! Taking a look ahead to
    peer you.

  2. After going over a number of the blog articles on your site, I seriously like your way
    of writing a blog. I book-marked it to my bookmark webpage list and will
    be checking back soon. Take a look at my website as well and let me know how you feel.

  3. Hey there just wanted to give you a quick heads up. The
    words in your post seem to be running off the screen in Safari.

    I’m not sure if this is a formatting issue or something
    to do with browser compatibility but I thought I’d post to let you know.
    The design look great though! Hope you get the problem resolved soon.
    Thanks

  4. I am really inspired together with your writing abilities
    and also with the layout for your weblog. Is this a paid theme or did you customize it your self?

    Anyway keep up the excellent quality writing, it’s rare to look a great weblog like this one today..

  5. Do you have a spam problem on this site; I also am
    a blogger, and I was curious about your situation;
    many of us have developed some nice practices and we are looking
    to trade strategies with others, be sure to shoot me an e-mail if interested.

  6. Hey! Would you mind if I share your blog with my twitter group?
    There’s a lot of folks that I think would really enjoy your
    content. Please let me know. Many thanks

  7. Hey there would you mind stating which blog platform you’re working with?
    I’m going to start my own blog soon but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems
    different then most blogs and I’m looking for something
    unique. P.S My apologies for being off-topic but I had to ask!

  8. Hi , I do believe this is an excellent blog. I stumbled upon it on Yahoo , i will come back once again. Money and freedom is the best way to change, may you be rich and help other people.

  9. When I initially commented I clicked the “Notify me when new comments are added” checkbox and
    now each time a comment is added I get several e-mails with the same comment.
    Is there any way you can remove people from that service?

    Many thanks!

  10. Hello there! I know this is kinda off topic however , I’d figured I’d
    ask. Would you be interested in trading links or maybe guest authoring a blog article or vice-versa?

    My blog goes over a lot of the same subjects as yours and I believe
    we could greatly benefit from each other. If you’re interested
    feel free to shoot me an email. I look forward to hearing from you!

    Terrific blog by the way!

  11. I do not even understand how I ended up right here, however I believed this submit was great.
    I do not recognise who you are however certainly you’re going to a well-known blogger if you aren’t
    already. Cheers!

  12. Hi there! Would you mind if I share your blog with my twitter group?

    There’s a lot of folks that I think would really
    enjoy your content. Please let me know. Thank you

  13. My brother recommended I may like this web site.
    He used to be entirely right. This submit truly made my day.
    You can not consider just how a lot time I had spent for this information! Thanks!

  14. Wonderful blog! Do you have any hints for aspiring writers?

    I’m hoping to start my own blog soon but I’m a little
    lost on everything. Would you recommend starting with a
    free platform like WordPress or go for a paid option? There are so
    many choices out there that I’m totally overwhelmed ..
    Any recommendations? Thank you!

  15. Hello there! This is kind of off topic but I need some guidance from an established blog.
    Is it tough to set up your own blog? I’m not very techincal but I can figure things out pretty
    fast. I’m thinking about creating my own but I’m not sure where
    to begin. Do you have any tips or suggestions?
    With thanks

Leave a Reply to http://asksylphoflight.tumblr.com Cancel reply

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