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. 算法笔记_073:哈密顿回路问题(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 什么是哈密顿回路? 引用自百度百科: 哈密顿图(哈密尔顿图)(英语:Hamiltonian path,或Traceable path)是一个无向图, ...

  2. 获取ping的最短、最长、平均时间

    # -*- coding: utf-8 -*- import osimport rep = os.popen('ping 120.26.77.101') out = p.read()regex = r ...

  3. 关于JSP乱码问题

    关于jsp乱码问题的解决. 1 最基本的乱码问题. 这个乱码问题是最简单的乱码问题.一般新会出现.就是页面编码不一致导致的乱码. <%@ page language="java&quo ...

  4. Errors occurred during the build

    Errors occurred during the build.Errors running builder 'Integrated External Tool Builder' on projec ...

  5. Visual studio之C# 重新定义Messbox的显示窗口位置

    背景 当前做的APP需要新建一个设置窗口,该设置窗口会出现在靠近屏幕边缘位置,但主窗口铺满屏幕,设置窗口会弹出一些讯息,但默认情况下Messagebox窗口会居中于主窗口,这不太符合要求,正常应该居中 ...

  6. Ubuntu 安装配置 JDK+Tomcat+Nginx

    安装配置JDK 下载安装 # 下载: wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=acc ...

  7. LNMP架构四

    php-fpm的pool(连接池) 我们查看php的进程时,会发现,在最后一个pool的选项,而这个就是我们在php-fpm配置文件里写的一个连接池. [root@bogon linux.com]# ...

  8. Easyui 二级菜单

    <div class="fitem"> <label>所在城市:</label> <input id="cityId" ...

  9. 微信模板消息php

    微信的模板消息需要认证的公众号后台申请 申请通过后就可以用平台定义的消息模板了 define('IN_ECS', true); require(dirname(__FILE__) . '/includ ...

  10. POJ-1318(list.sort()输出不为字典序,map才是按字典序排列)

    #include<iostream> #include<string> #include<list> #include<map> #include< ...