题目描述

Nowadays, "Circle of Friends" is a very popular social networking platform in WeChat. We can share our life to friends through it or get other's situation.

Similarly, in real life, there is also a circle of friends, friends would often get together communicating and playing to maintain friendship. And when you have difficulties, friends will generally come to help and ask nothing for return.

However, the friendship above is true friend relationship while sometimes you may regard someone as your friend but he doesn't agree.In this way when you ask him for help, he often asks you for a meal, and then he will help you.

If two people think they are friends mutually,they will become true friend,then once one of them has a problem or makes a query, the other one will offer help for free.What's more,if one relationship is similar to “A regards B as friend, B regards C as friend
and C regards A as friend”,they will make a friends circle and become true friends too with each other. Besides, people will not ask those who they don’t regard as friends for help. If one person received a question and he can not solve it, he will ask his
friends for help. 

Now, Nias encounters a big problem, and he wants to look for Selina's help. Given the network of friends, please return the minimum number of meals Nias must offer. Of course Nias is lavish enough, so he will pay for all the meals in the network of friends.

输入

The first line of input contains an integer T, indicating the number of test cases (T<=30).

For each test case, the first line contains two integers, N and M represent the number of friends in the Nias’s network and the number of relationships in that network. N and M are less than 100000 and you can assume that 0 is Nias and n-1 is Selina.

Next M lines each contains two integers A and B, represent a relationship that A regards B as his friend, A and B are between 0 and n-1.

输出

For each test case, please output the minimum number of meals Nias need to offer; if Nias can’t get Selina’s help, please output -1.

样例输入

3
4 4
0 1
1 2
2 1
2 3

3 3
0 1
1 2
2 1

3 1
0 1

样例输出

2
1
-1

题解

首先,深度优先求有向图强连通分量(Tarjan)算法

其次,构图,所构造出来的新图是一个拓扑图

最后,spfa求最短路径.

知识点:

spfa适用于稀疏图最短路径,dijstra适合稠密图最短路.

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 7;
int N, M;
struct Edge{
	int to, next;
}e[maxn*2];
int ei,g[maxn];
void push_back(int f, int t){
	e[++ei].to = t;
	e[ei].next = g[f];
	g[f] = ei;
}
int ti, pre[maxn], low[maxn],num[maxn],sccNo;
int a[maxn];
struct Stack{
	int a[maxn];
	int i;
	void init(){ i = 0; }
	void push(int x){a[i++] = x;}
	int pop(){return a[--i];}
}sta;
void dfs(int now){
	pre[now] = low[now] = ++ti;
	sta.push(now);
	for (int i = g[now]; i; i = e[i].next){
		int t = e[i].to;
		if (pre[t] == 0)dfs(t), low[now] = min(low[now], low[t]);
		else if (num[t] == 0)low[now] = min(low[now], pre[t]);
	}
	if (pre[now] == low[now]){
		++sccNo;
		while (true){
			int x = sta.pop();
			num[x] = sccNo;
			if (x == now)break;
		}
	}
}
void newGraph(){
	memset(a, 0, sizeof(int)*(1+sccNo));
	for (int i = 0; i < N; i++){
		for (int j = g[i]; j; j = e[j].next){
			int t = e[j].to;
			if (num[i] ^ num[t]){
				e[++ei].next = a[num[i]];
				e[ei].to = num[t];
				a[num[i]] = ei;
			}
		}
	}
}
struct Q{
	int a[maxn], head, rear;
	bool has[maxn];
	void init(){ head = rear = 0; memset(has, 0, sizeof(int)*(sccNo+1)); }
	void enq(int x){ a[rear] = x; rear = (rear + 1) % maxn; has[x] = 1; }
	int deq(){ int ans = a[head]; head = (head + 1) % maxn; has[ans] = 0; return ans; }
}q;
int dis[maxn];
void spfa(){
	q.init();
	q.enq(num[0]);
	memset(dis, 0x34, sizeof(int)*(1 + sccNo));
	dis[num[0]] = 0;
	while (q.head^q.rear){
		int now = q.deq();
		for (int i = a[now]; i; i = e[i].next){
			int t = e[i].to;
			if (dis[t] > dis[now] + 1){
				dis[t] = dis[now] + 1;
				if (q.has[t]== false){
					q.enq(t);
				}
			}
		}
	}
	if (dis[num[N - 1]] == 0x34343434)printf("-1\n");
	else printf("%d\n", dis[num[N - 1]]);
}
int main(){
	freopen("in.txt", "r", stdin);
	int T; scanf("%d", &T);
	while (T--){
		scanf("%d%d", &N, &M);
		ei = 0, memset(g, 0, sizeof(int)*N);
		while (M--){
			int x, y; scanf("%d%d", &x, &y);
			push_back(x, y);
		}
		ti = 0, memset(pre, 0, sizeof(int)*N), memset(num, 0, sizeof(int)*N);
		sta.init(), sccNo = 0;
		for (int i = 0; i < N; i++)if (pre[i] == 0)dfs(i);
		newGraph();
		spfa();
	}
	return 0;
}

东大oj-1591 Circle of friends的更多相关文章

  1. 东大OJ 2SAT 异或

    看了十年才懂懂了十年才会会了十年才会写写了十年才写完写完了十年才能改对 #include<stdio.h> #include<string.h> struct res{ int ...

  2. 东大OJ-Max Area

    1034: Max Area 时间限制: 1 Sec  内存限制: 128 MB 提交: 40  解决: 6 [提交][状态][讨论版] 题目描述 又是这道题,请不要惊讶,也许你已经见过了,那就请你再 ...

  3. 西南民大oj(两园交求面积)

    西南民大oj:http://www.swunacm.com/acmhome/welcome.do?method=index 我的几何不可能那么可爱 时间限制(普通/Java) : 1000 MS/ 3 ...

  4. Online Judge(OJ)搭建(第一版)

    搭建 OJ 需要的知识(重要性排序): Java SE(Basic Knowledge, String, FileWriter, JavaCompiler, URLClassLoader, Secur ...

  5. [翻译svg教程]svg中的circle元素

    svg中的<circle> 元素,是用来绘制圆形的,例如 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink= ...

  6. [C#] 逆袭——自制日刷千题的AC自动机攻克HDU OJ

    前言 做过杭电.浙大或是北大等ACM题库的人一定对“刷题”不陌生,以杭电OJ为例:首先打开首页(http://acm.hdu.edu.cn/),然后登陆,接着找到“Online Exercise”下的 ...

  7. oj Rapid Typing

    import bs4 import requests import urllib2 import time import base64 session=requests.Session() respo ...

  8. 在线OJ实用技巧(转载)

    1.一般用C语言节约空间,要用C++库函数或STL时才用C++; cout.cin和printf.scanf最好不要混用. 2.有时候int型不够用,可以用long long或__int64型(两个下 ...

  9. 设计一个程序,程序中有三个类,Triangle,Lader,Circle。

    //此程序写出三个类,triangle,lader,circle:其中triangle类具有类型为double的a,b,c边以及周长,面积属性, //具有周长,面积以及修改三边的功能,还有判断能否构成 ...

随机推荐

  1. 三、Android学习第三天——Activity的布局初步介绍(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 三.Android学习第三天——Activity的布局初步介绍 今天总结下 ...

  2. Android开发中的Json字符串与复杂的嵌套对象互转。

    Gson 可能是大家都觉得比较简单吧.我发现用JSONObject和网上下载的JSONHelper类使用起来很无语,只能解析简单的单层对象,如果有嵌套的就不能直转转成可用对象了.所以网上找了一会儿,发 ...

  3. TCP校验和的原理和实现

        http://blog.csdn.net/zhangskd/article/details/11770647 分类: Linux TCP/IP Linux Kernel 2013-09-24 ...

  4. CentOS 6.3下rsync服务器的安装与配置

    一.rsync 简介 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件,也可以使用 Rsync 同步本地硬盘中的不同目录. Rsy ...

  5. 大话设计模式C++版——工厂方法模式

    工厂方法模式是以简单工厂模式为基础的,如果未了解简单工厂模式的同学可先浏览<大话设计模式C++版——简单工厂模式>.在简单工厂模式中,提到过简单工厂模式的缺陷,即违背了开发—封闭原则,其主 ...

  6. C#基础---扩展方法的应用

     最近对扩展方法比较感兴趣,就看了看资料,记录一下扩展方法的几种方法. 一. 扩展方法的基本使用: Note: 1. 扩展方法必须在静态类中, 2 扩展方法必须声明静态方法,3 扩展方法里面不能调用其 ...

  7. Stanford机器学习笔记-5.神经网络Neural Networks (part two)

    5 Neural Networks (part two) content: 5 Neural Networks (part two) 5.1 cost function 5.2 Back Propag ...

  8. AC日记——字符串位移包含问题 1.7 19

    19:字符串移位包含问题 总时间限制:  1000ms 内存限制:   65536kB 描述 对于一个字符串来说,定义一次循环移位操作为:将字符串的第一个字符移动到末尾形成新的字符串. 给定两个字符串 ...

  9. NOIP2015提高组Day1 Message

    题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的生日.之后每一 ...

  10. gnuplot 的安装

    需要同时安装gnuplot和gnuplot-x11才能画出图 sudo apt-get install gnuplot gnuplot-x11 gnuplot not showing the grap ...