http://www.lydsy.com/JudgeOnline/problem.php?id=3296

显然,每群能交流的群是个强联通块

然后求出scc的数量,答案就是scc-1

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=50005;
int n, m, ihead[N], cnt, FF[N], LL[N], tot, q[N], top, vis[N], scc;
struct ED { int to, next; }e[N*10];
void add(int u, int v) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u;
}
void tarjan(int x) {
vis[x]=1; FF[x]=LL[x]=++tot;
q[++top]=x;
int v;
for(int i=ihead[x]; i; i=e[i].next) {
v=e[i].to;
if(!FF[v]) tarjan(v), LL[x]=min(LL[x], LL[v]);
else if(vis[v]) LL[x]=min(LL[x], FF[v]);
}
if(FF[x]==LL[x]) {
++scc;
do {
v=q[top--];
vis[v]=0;
}while(v!=x);
}
} int main() {
read(n); read(m);
for1(i, 1, n) {
int t=getint();
while(t--) {
int v=getint();
add(i, n+v);
}
}
for1(i, 1, n) if(!FF[i]) tarjan(i);
print(scc-1);
return 0;
}

Description

农夫约翰的N(2 <= N<=10,000)头奶牛,编号为1.. N,一共会流利地使用M(1<= M
<=30,000)种语言,编号从1 .. M.,第i头,会说K_i(1 <= K_i<= M)种语言,即L_i1,
L_i2,..., L_{iK_i} (1 <= L_ij <= M)。 FJ的奶牛不太聪明,所以K_i的总和至多为100,000。

两头牛,不能直接交流,除非它们都会讲某一门语言。然而,没有共同语言的奶牛们,可以让其它的牛给他们当翻译。换言之,牛A和B可以谈话,当且仅当存在一
个序列奶牛T_1,T_2,...,T_k,A和T_1都会说某一种语言,T_1和T_2也都会说某一种语言……,并且T_k和B会说某一种语言。
农夫约翰希望他的奶牛更加团结,所以他希望任意两头牛之间可以交流。他可以买书教他的奶牛任何语言。作为一个相当节俭的农民,FJ想要购买最少的书籍,让所有他的奶牛互相可以说话。
帮助他确定:
    *他必须购买的书籍的最低数量

Input

*第1行:两个用空格隔开的整数:N和M
*第2.. N +1行:第i +1行描述的牛i的语言,K_i+1个空格隔开的整数:K_i L_i1
        L_i2,...,L_I{K_i}。

Output

*第1行:一个整数,FJ最少需要购买的书籍数量。

	

Sample Input

3 3
2 3 2
1 2
1 1

Sample Output

1

HINT

给三号牛买第二本书即可

Source

【BZOJ】3296: [USACO2011 Open] Learning Languages(tarjan)的更多相关文章

  1. 【BZOJ】3300: [USACO2011 Feb]Best Parenthesis(模拟)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3300 这个细节太多QAQ 只要将所有的括号'('匹配到下一个')'然后dfs即可 简单吧,,, #i ...

  2. 【BZOJ】3053: The Closest M Points(kdtree)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...

  3. 【BZOJ】3301: [USACO2011 Feb] Cow Line(康托展开)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3301 其实这一题很早就a过了,但是那时候看题解写完也是似懂非懂的.... 听zyf神犇说是康托展开, ...

  4. 【BZOJ】3668: [Noi2014]起床困难综合症(暴力)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3668 这题很简单.............. 枚举每一位然后累计即可.. QAQ,第一次以为能1A, ...

  5. 【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3223 默默的.. #include <cstdio> #include <cstr ...

  6. 【BZOJ】1602: [Usaco2008 Oct]牧场行走(lca)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1602 一开始以为直接暴力最短路,但是n<=1000, q<=1000可能会tle. 显然 ...

  7. 【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1601 很水的题,但是一开始我看成最短路了T_T 果断错. 我们想,要求连通,对,连通!连通的价值最小 ...

  8. 【BZOJ】1600: [Usaco2008 Oct]建造栅栏(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1600 说好的今天开始刷水.. 本题一开始我以为是排列组合,但是自己弱想不出来,只想到了如果四边有一条 ...

  9. 【BZOJ】1503: [NOI2004]郁闷的出纳员(Splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1503 这题没有看题解就1a了-好开心,, 其实后面去看题解发现他们的都很麻烦,其实有种很简单的做法: ...

随机推荐

  1. 从word中导出图片

    想把word里面的图片导出来,可以这么操作: 1.右键word里面的图片,复制 2.打开电脑的画图工具,粘贴,然后保存

  2. pip运行报错Fatal error in launcher: Unable to create process using pip.exe

    使用pip的时候报错Fatal error in launcher: Unable to create process using pip.exe 解决办法,升级pip python -m pip i ...

  3. PHP中is_*() 函数用法

    PHP中is_*() 函数用法 is_a - 如果对象属于该类或该类是此对象的父类则返回 TRUE is_array - 检测变量是否是数组 is_bool - 检测变量是否是布尔型 is_calla ...

  4. js中求水仙花数

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Customize User Interfaces and Pass User Input to Installer Classes

    In this article I am going to demonstrate how to customize your MSI install to prompt the user for s ...

  6. 将Cmder添加到系统右键菜单中

    1.把 Cmder 加到环境变量 把Cmder.exe存放的目录添加到系统环境变量: 加完之后,Win+r一下输入cmder,即可. 2.添加 cmder 到右键菜单:环境变量添加后,在任意文件夹中即 ...

  7. JVM虚拟机(二):堆、栈、方法区概念区别

    Java 堆 Java堆是和Java应用程序关系最密切的内存空间,几乎所有的对象都放在其中,并且Java堆完全是自动化管理,通过垃圾收集机制,垃圾对象会自动清理,不需自己去释放. 根据垃圾回收机制的不 ...

  8. Centos下源码安装git

    1.centos下git版本太久了,才1.8几,而官方更新的还是很活跃的,于是我就想源码安装一个新版本. 2.首先到: https://github.com/git/git/releases 下载最新 ...

  9. strlen函数实现

    原型: int strlen(const char *s); 作用:返回字符串的长度. 方法1:利用中间变量 int strlen(const char *s){ ; while(s[i] != '\ ...

  10. python学习之count()

    定义: count()方法用于统计对象中,某个字符出现的次数 语法: str.count(sub, start= ,end=len(string)) sub:搜索的对象 start和end:搜索的范围 ...