Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph
D - Mr. Kitayuta's Colorful Graph
思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙。。
对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于大于sqrt(n)的块,我们暴力枚举答案。
这样就能做到严格sqrt(n) * n
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg using namespace std; const int N = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int n, m, cnt, fa[N], a[N], b[N], c[N];
unordered_map<int, bool> mp[N];
map<PII, int> ans;
vector<PII> vec[N];
int getRoot(int x) {
return fa[x] == x ? x : getRoot(fa[x]);
} int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i++) {
scanf("%d%d%d", &a[i], &b[i], &c[i]);
vec[c[i]].push_back(mk(a[i], b[i]));
}
for(int i = ; i <= n; i++) fa[i] = i;
for(int i = ; i <= m; i++) {
map<int, int> ma;
for(PII t : vec[i]) {
int x = getRoot(t.fi), y = getRoot(t.se);
if(x != y) fa[x] = y;
}
for(PII t : vec[i]) {
int x = getRoot(t.fi), y = getRoot(t.se);
if(ma.find(x) != ma.end()) {
mp[t.fi][ma[x]] = true;
} else {
cnt++;
mp[t.fi][cnt] = true;
ma[x] = cnt;
}
if(ma.find(y) != ma.end()) {
mp[t.se][ma[y]] = true;
} else {
cnt++;
mp[t.se][cnt] = true;
ma[y] = cnt;
}
}
for(PII t : vec[i]) {
fa[t.fi] = t.fi;
fa[t.se] = t.se;
}
} int q; scanf("%d", &q);
while(q--) {
int u, v;
scanf("%d%d", &u, &v);
if(mp[u].size() > mp[v].size()) swap(u, v);
if(ans.find(mk(u, v)) != ans.end()) {
printf("%d\n", ans[mk(u, v)]);
} else {
int num = ;
for(auto t : mp[u]) {
if(mp[v].find(t.fi) != mp[v].end()) num++;
}
printf("%d\n", num);
ans[mk(u, v)] = num;
}
}
return ;
} /*
*/
Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph的更多相关文章
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- 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. 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的应用)
由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一 ...
随机推荐
- git 撤销上一次 commit
1.本地 commit,没有推到远程仓库 可以 git reset --soft <commit_id>,commit_id 是要回退到的某一版本 然后再进行修改,再commit, 如果需 ...
- jre,jdk,jvm的关系
今天在用maven搭建项目工程的时候出错的原因竟然是因为使用了jre,而非jdk导致报错,这里就搜集了有关这方面的信息: JDK(Java Development Kit)是针对Java开发员的产 ...
- MakeDown的使用
Makedown的使用 之前有用博客园来写博客,但是因为它的界面不好看,所以中途就放弃了.后来也使用过"有道云笔记",发现其写的笔记的界面很简洁工整.有道云笔记的书写原理和Make ...
- 2.aop中几个注解的含义
参考地址:http://elim.iteye.com/blog/2395255
- 817C. Really Big Numbers 二分
LINK 题意:给出两个数n, s,要求问1~n中\(x-bit(x)>=s\)的数有多少个.其中bit(x)指x的各位数之和 思路:首先观察能够发现,对于一个数如果满足了条件,由于x-bit( ...
- 前端观察:HTML5做得好,收入不比 iOS 差
谷歌奋进的绿色机器人超过了苹果的市场占有率,这一市场现象,导致了愈来愈多的开发者转投html5的怀抱.而HTML5将成为开发者拥抱同时拥抱Android和IOS的不二法门. 根据著名移动生态系统分析公 ...
- MQTT协议及推送服务(二)
MQTT简介 MQTT全称叫做Message Queuing Telemetry Transport,意为消息队列遥测传输,是IBM开发的一个即时通讯协议.由于其维护一个长连接以轻量级低消耗著称,所以 ...
- XML-RPC笔记
1.什么是XML-RPC RPC(Remote Procedure Call)就是相当于提供了一种"远程接口"来供外部系统调用,常用于不同平台.不同架构的系统之间互相调用. XML ...
- zepto.js 实现原理解析
zepto 是移动端常用的 dom 库,代码轻巧,操作方式类同 jquery.那么 zepto 的核心实现原理是什么呢?
- Windows 7 64 位操作系统安装 Ubuntu 17.10
一.准备工作 1. DiskGenius:分区工具,为 Linux 建立单独的分区.(Linux 公社下载源) 2. UUI:Universal USB Installer,通用 U 盘安装器,用来制 ...