Circle of Friends

Time Limit: 2000 ms Memory Limit: 65536 KiB

Problem Description

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.

Input

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.

Output

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.

Sample Input

3
4 4
0 1
1 2
2 1
2 3 3 3
0 1
1 2
2 1 3 1
0 1

Sample Output

2
1
-1

Hint

 

Source

“浪潮杯”山东省第六届ACM大学生程序设计竞赛
 
水强连通
缩点建图求就好了
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int n, m, s, t;
vector<int> G[maxn]; int pre[maxn], low[maxn], sccno[maxn], dfs_clock, scc_cnt;
stack<int> S;
int nex[maxn << ], head[maxn], cnt; void dfs(int u)
{
pre[u] = low[u] = ++dfs_clock;
S.push(u);
for(int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if(!pre[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else if(!sccno[v])
low[u] = min(low[u], pre[v]); }
if(low[u] == pre[u])
{
scc_cnt++;
for(;;)
{
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} struct node
{
int u, v;
}Node[maxn << ]; void add(int u, int v)
{
Node[cnt].u = u;
Node[cnt].v = v;
nex[cnt] = head[u];
head[u] = cnt++; }
int mx;
bool flag;
void dfs2(int u, int d)
{
// cout << u << endl;
d += ;
if(u == sccno[n - ])
{
flag = ;
mx = min(mx, d);
return;
}
for(int i = head[u]; i != -; i = nex[i])
{
int v = Node[i].v;
// cout << v << endl;
dfs2(v, d);
}
} int main()
{
int T;
rd(T);
while(T--)
{
mem(sccno, );
mem(pre, );
mem(head, -);
cnt = ;
flag = ;
mx = INF;
dfs_clock = scc_cnt = ;
rd(n), rd(m);
for(int i = ; i < n; i++) G[i].clear();
for(int i = ; i < m; i++)
{
int u, v;
rd(u), rd(v);
G[u].push_back(v);
}
for(int i = ; i < n; i++)
if(!pre[i]) dfs(i);
for(int i = ; i < n; i++)
for(int j = ; j < G[i].size(); j++)
{
int v = G[i][j];
if(sccno[i] != sccno[v])
add(sccno[i], sccno[v]);
}
s = sccno[];
// cout << s << endl;
dfs2(s, -);
if(flag)
pd(mx);
else
printf("-1\n"); } return ;
}

第六届SD省赛 Circle of Friends的更多相关文章

  1. 第六届acm省赛总结(退役贴)

    前言: 这是我的退役贴,之前发到了空间里,突然想到也要在博客里发一篇,虽然我很弱,但是要离开了还是有些感触,写出来和大家分享一下,希望不要见笑.回来看看,这里也好久没有更新了,这一年确实有些懈怠,解题 ...

  2. 山东省第六届ACM省赛

    A.Nias and Tug-of-War(sort排序) B.Lowest Unique Price(set+map) C.Game!(博弈) D.Stars E.BIGZHUGOD and His ...

  3. 山东省第六届ACM省赛 H---Square Number 【思考】

    题目描述 In mathematics, a square number is an integer that is the square of an integer. In other words, ...

  4. 第六届蓝桥杯软件类省赛题解C++/Java

    第六届蓝桥杯软件类省赛题解C++/Java 1[C++].统计不含4的数字统计10000至99999中,不包含4的数值个数.答:暴力循环范围内所有数字判断一下就是了,答案是52488 1[Java]. ...

  5. 2015年第六届蓝桥杯C/C++B组省赛题目解析

    一.奖券数目 有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利.虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求.某抽奖活动的奖券号码是5位数(10000-99999),要求其中 ...

  6. 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)

    Biggest Number 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...

  7. 第九届蓝桥杯国赛+第二天的第11届acm省赛的总结

    第九届蓝桥杯国赛+第二天的第11届acm省赛的总结 25号坐的去北京的火车,10个小时的火车,然后挤了快两个小时的地铁,最终达到了中国矿业大学旁边的订的房间.12个小时很难受,晕车症状有点严重,吃了快 ...

  8. 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛

    比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...

  9. 小编接地气——第六届中国云计算大会攻略Q&amp;A

    2014年5月20-23日,第六届中国云计算大会在北京召开. 花个1000多元,在工作日请假来參加大会,不能让大家白跑一趟而是物有所值. 小编写了大会攻略Q&A,分享给各位 Q:为什么要參加关 ...

随机推荐

  1. [转]Have a query in Blue prism coding stage and collection stage.

    本文转自:https://www.rpaforum.net/threads/have-a-query-in-blueprism-coding-stage-and-collection-stage.48 ...

  2. [转] 以后再有人问你selenium是什么,你就把这篇文章给他

    本文转自:https://blog.csdn.net/TestingGDR/article/details/81950593 写在最前面:目前自动化测试并不属于新鲜的事物,或者说自动化测试的各种方法论 ...

  3. 20190422-外部导入CSS样式之link、CSS@import、Sass分音

    写在前面乱七八糟的前言:今天wuliMR黄问了一个问题,Sass的分音与link标签都是导入外部样式的,有什么不同,这真是个好问题,因为本白着实没想过,也不知道,不过没关系,成功的背后总有一个默默无闻 ...

  4. 超简单的canvas绘制地图

        本文使用geojson数据,通过缩放和平移把地图的地理坐标系转换canvas的屏幕坐标系,然后将转换后的数据绘制到canvas上.     首先要计算数据的最大最小值,遍历所有坐标点的最大最小 ...

  5. thymeleaf th:href url传递多参数

    <a th:href="@{/teacherShowMember(class_id=${class.classId},class_name=${class.className})}&q ...

  6. Android Studio教程10-Intent的详细使用

    目录 1. Intent启动器 1.1. Intent的用途 1. 启动Activity 3. 传递广播 1.2. Intent类型 1.显示Intent 2.隐式Intent 2. 构建Intent ...

  7. 用jenkins创建节点

    原料:(1)jre下载链接:https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html ( ...

  8. 基于CAS在.NET中实现SSO单点登录

    单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 单点登录原理 ...

  9. 记录Nginx实现内容重定向的两种方式:rewrite和反向代理

    第一种:rewrite server { listen                80; server_name   www.test.org; root                  /te ...

  10. Scrapy框架-CrawlSpider

    目录 1.CrawlSpider介绍 2.CrawlSpider源代码 3. LinkExtractors:提取Response中的链接 4. Rules 5.重写Tencent爬虫 6. Spide ...