【BZOJ】3296: [USACO2011 Open] Learning Languages(tarjan)
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
2 3 2
1 2
1 1
Sample Output
HINT
给三号牛买第二本书即可
Source
【BZOJ】3296: [USACO2011 Open] Learning Languages(tarjan)的更多相关文章
- 【BZOJ】3300: [USACO2011 Feb]Best Parenthesis(模拟)
http://www.lydsy.com/JudgeOnline/problem.php?id=3300 这个细节太多QAQ 只要将所有的括号'('匹配到下一个')'然后dfs即可 简单吧,,, #i ...
- 【BZOJ】3053: The Closest M Points(kdtree)
http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...
- 【BZOJ】3301: [USACO2011 Feb] Cow Line(康托展开)
http://www.lydsy.com/JudgeOnline/problem.php?id=3301 其实这一题很早就a过了,但是那时候看题解写完也是似懂非懂的.... 听zyf神犇说是康托展开, ...
- 【BZOJ】3668: [Noi2014]起床困难综合症(暴力)
http://www.lydsy.com/JudgeOnline/problem.php?id=3668 这题很简单.............. 枚举每一位然后累计即可.. QAQ,第一次以为能1A, ...
- 【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=3223 默默的.. #include <cstdio> #include <cstr ...
- 【BZOJ】1602: [Usaco2008 Oct]牧场行走(lca)
http://www.lydsy.com/JudgeOnline/problem.php?id=1602 一开始以为直接暴力最短路,但是n<=1000, q<=1000可能会tle. 显然 ...
- 【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=1601 很水的题,但是一开始我看成最短路了T_T 果断错. 我们想,要求连通,对,连通!连通的价值最小 ...
- 【BZOJ】1600: [Usaco2008 Oct]建造栅栏(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1600 说好的今天开始刷水.. 本题一开始我以为是排列组合,但是自己弱想不出来,只想到了如果四边有一条 ...
- 【BZOJ】1503: [NOI2004]郁闷的出纳员(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1503 这题没有看题解就1a了-好开心,, 其实后面去看题解发现他们的都很麻烦,其实有种很简单的做法: ...
随机推荐
- android之Activity.startManagingCursor方法详解
在使用数据库操作查询数据后,如果是在Activity里面处理,那么很可能就会用到startManagingCursor()方法,在这里讲一下它的作用和使用注意事项. 调用这个方法,就是将获得的Curs ...
- Hibernate关系映射(三) 多对一和一对多
一.多对一 学生Student和班级Grade实现多对一,多个学生对应一个班级. Student.java实体类,映射了班级的属性. package com.lxit.entity; import j ...
- swagger 生成的接口文档,隐藏接口的某个参数
[问题描述] controller 中的处理请求的方法,有时候会添加一些额外的参数.比如下面代码中 UserVo: @PostMapping(value = "/add-office-par ...
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...
- 最全ASCII对应码表-键值
OCT(八进制) 最全ASCII码对应表—与键盘按键对应值 (二进)Bin (十进)Dec (十六进)Hex 缩写/字符 ...
- 微信小程序挑一挑辅助
1.窗体 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;usi ...
- Js日常笔记之this
在javascript中自己创建构造函数时可以利用this来指向新创建的对象上.这样就可以避免函数中的this指向全局了,如下 var x = 2; function test(){ this.x = ...
- 特征组合&特征交叉
https://segmentfault.com/a/1190000014799038 https://www.jianshu.com/p/fc96675b6f8e https://blog.csdn ...
- 安装Spring+搭建Spring开发环境
https://blog.csdn.net/csdnsjg/article/details/80152815 https://jingyan.baidu.com/article/219f4bf798e ...
- setTime
var getTime = function() { var _ = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09'], //补 ...