DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
/*
题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条
DFS:暴力每个颜色,以u走到v为结束标志,累加条数
注意:无向图
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
using namespace std; const int MAXN = 1e2 + ;
const int INF = 0x3f3f3f3f;
int n, m;
bool vis[MAXN];
vector<pair<int, int> > V[MAXN]; bool DFS(int u, int v, int c)
{
vis[u] = true;
if (u == v) return true;
for (int i=; i<V[u].size (); ++i)
{
pair<int, int> p = V[u][i];
if (p.second == c && !vis[p.first])
{
if (DFS (p.first, v, c)) return true;
}
} return false;
} int main(void) //Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
{
//freopen ("B.in", "r", stdin); while (scanf ("%d%d", &n, &m) == )
{
for (int i=; i<=m; ++i)
{
int u, v, c;
scanf ("%d%d%d", &u, &v, &c);
V[u].push_back (make_pair (v, c));
V[v].push_back (make_pair (u, c));
}
int q; scanf ("%d", &q);
while (q--)
{
int u, v; scanf ("%d%d", &u, &v);
int ans = ;
for (int i=; i<=m; ++i)
{
memset (vis, false, sizeof (vis));
if (DFS (u, v, i)) ans++;
}
printf ("%d\n", ans);
}
} return ;
}
/*
并查集:开在结构体的并查集,进行如下的两个操作
uf[c].Union (u, v); uf[i].same (u, v)
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std; const int MAXN = 1e2 + ;
const int INF = 0x3f3f3f3f;
struct UF
{
int rt[MAXN];
void init(void) {memset (rt, -, sizeof (rt));} int Find(int x) {return (rt[x] == -) ? x : rt[x] = Find (rt[x]);} void Union(int x, int y)
{
int tx = Find (x);
int ty = Find (y);
if (tx > ty) rt[ty] = tx;
else if (tx < ty) rt[tx] = ty;
} bool same(int x, int y)
{
return (Find (x) == Find (y));
}
}uf[MAXN];
int n, m; int main(void) //Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
{
//freopen ("B.in", "r", stdin); while (scanf ("%d%d", &n, &m) == )
{
for (int i=; i<=m; ++i) uf[i].init ();
for (int i=; i<=m; ++i)
{
int u, v, c;
scanf ("%d%d%d", &u, &v, &c);
uf[c].Union (u, v);
}
int q; scanf ("%d", &q);
while (q--)
{
int u, v; scanf ("%d%d", &u, &v);
int ans = ;
for (int i=; i<=m; ++i)
{
if (uf[i].same (u, v)) ans++;
}
printf ("%d\n", ans);
}
} return ;
}
并查集
DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph的更多相关文章
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集
D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)
数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph
D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...
- 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift
题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...
- CF 286(div 2) B Mr. Kitayuta's Colorful Graph【传递闭包】
解题思路:给出n个点,m条边(即题目中所说的两点之间相连的颜色) 询问任意两点之间由多少种不同的颜色连接 最开始想的时候可以用传递闭包或者并查集来做,可是并查集现在还不会做,就说下用传递闭包来做的这种 ...
- Codeforces Round #286 (Div. 1) B. Mr. Kitayuta's Technology (强连通分量)
题目地址:http://codeforces.com/contest/506/problem/B 先用强连通判环.然后转化成无向图,找无向图连通块.若一个有n个点的块内有强连通环,那么须要n条边.即正 ...
- Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP
题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...
- Codeforces Round #286 (Div. 2)A. Mr. Kitayuta's Gift(暴力,string的应用)
由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一 ...
随机推荐
- 《写给大忙人看的java》笔记--基本的编程结构
1.字符串是UTF-16编码中的Unicode编码点的序列 2.绑定System.in的Scanner可以读取终端输入: Scanner sc = new Scanner(System.in); 3. ...
- 使用NetBeans生成jar包,并在jar包中添加资源
在NetBeans中,执行Clean and Build便可得到jar文件 若要在jar中添加资源,先用压缩软件打开jar,然后将资源拖进当前归档文件即可 使用Class.getResource(St ...
- ubuntu 本地和服务器scp文件传输
安装 SSH(Secure Shell) 服务以提供远程管理服务 sudo apt-get install ssh SSH 远程登入 Ubuntu 机 ssh username@192.168.0.1 ...
- linux 一个超简单的makefile
makefile 自动化变量: $@ : 规则的目标文件名 例如:main:main.o test.o g++ -Wall -g main.o test. ...
- magento导入数据的方法
导入演示数据 分两种情况处理. 如果你是用composer方式安装的 非常简单,二行命令搞定:在项目根目录下执行.我们的是在/var/www/magento2/下面. 安装演示数据 php bin/m ...
- 西交校赛 I. GZP and CS(数位dp)
I. GZP and CS GZP love to play Counter-Strike(CS). One day GZP was playing a mod of CS. The counter- ...
- 「UVA524」 Prime Ring Problem 质数环
Description 输入正整数n,把整数1,2,-,n组成一个环,使得相邻两个整数之和均为素数.输出时,从整数1开始逆时针排列.同一个环恰好输出一次.n<=16. A ring is com ...
- 利用PyCharm的Profile工具进行Python性能分析
Profile:PyCharm提供了性能分析工具Run->Profile,如下图所示.利用Profile工具可以对代码进行性能分析,找出瓶颈所在. 测试:下面以一段测试代码来说明如何使用pych ...
- centos6.5下忘记mysql密码
1.此方式会使得服务器处于不安全的情况,请尽量保证在安全的环境下进行,因为,此方式,会使得任何人任意地连接MySQL数据库. 2.#vim /etc/my.cnf 在[mysqld]的段中,加上ski ...
- Spring 3.1新特性之一:使用Spring Profile和Mybatis进行多个数据源(H2和Mysql)的切换
最近在做WebMagic的后台,遇到一个问题:后台用到了数据库,本来理想情况下是用Mysql,但是为了做到开箱即用,也整合了一个嵌入式 数据库H2.这里面就有个问题了,如何用一套代码,提供对Mysql ...