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的更多相关文章

  1. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  2. 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/ ...

  3. 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 ...

  4. Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)

    数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...

  5. 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift

    题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...

  6. CF 286(div 2) B Mr. Kitayuta's Colorful Graph【传递闭包】

    解题思路:给出n个点,m条边(即题目中所说的两点之间相连的颜色) 询问任意两点之间由多少种不同的颜色连接 最开始想的时候可以用传递闭包或者并查集来做,可是并查集现在还不会做,就说下用传递闭包来做的这种 ...

  7. Codeforces Round #286 (Div. 1) B. Mr. Kitayuta&#39;s Technology (强连通分量)

    题目地址:http://codeforces.com/contest/506/problem/B 先用强连通判环.然后转化成无向图,找无向图连通块.若一个有n个点的块内有强连通环,那么须要n条边.即正 ...

  8. Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP

    题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...

  9. Codeforces Round #286 (Div. 2)A. Mr. Kitayuta's Gift(暴力,string的应用)

    由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一 ...

随机推荐

  1. 5种网络通信设计模型(也称IO模型)

    1.基本概念 同步:同步函数一般指调用函数后,等到函数功能实现再返回,期间一直霸占的CPU,等待期间同一个线程无法执行其他函数 异步:异步函数指调用函数后,不管函数功能是否实现,立马返回:通过回调函数 ...

  2. (转)linux下vi命令修改文件及保存的使用方法

    进入vi的命令         vi filename :打开或新建文件,并将光标置于第一行首    vi n filename :打开文件,并将光标置于第n行首    vi filename :打开 ...

  3. 树的性质和dfs的性质 Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E

    http://codeforces.com/contest/782/problem/E 题目大意: 有n个节点,m条边,k个人,k个人中每个人都可以从任意起点开始走(2*n)/k步,且这个步数是向上取 ...

  4. HDU 6211 卡常数取模 预处理 数论

    求所有不超过1e9的 primitive Pythagorean triple中第2大的数取模$2^k$作为下标,对应a[i]数组的和. 先上WIKI:https://en.wikipedia.org ...

  5. DIV+CSS制作斜线效果记录

    DIV+CSS 斜线效果很简单,只需设置一下CSS Border 的边框就能有斜线效果.代码分享给大家,你可以自己变通. 提示要注意两点:1.DIV宽高的定义.2.DIV在 IE6 中默认是有高度的. ...

  6. [acmm week12]染色(容斥定理+组合数+逆元)

    1003 染色         Time Limit: 1sec    Memory Limit:256MB Description 今天离散数学课学了有关树的知识,god_v是个喜欢画画的人,所以他 ...

  7. select多选框

    select多选框 效果: 代码: <HTML> <HEAD> <TITLE>选择下拉菜单</TITLE> <meta http-equiv=&q ...

  8. Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅

    在上一篇Spring中使用JdbcTemplate访问数据库中介绍了一种基本的数据访问方式,结合构建RESTful API和使用Thymeleaf模板引擎渲染Web视图的内容就已经可以完成App服务端 ...

  9. 【leetcode 简单】第三十七题 相交链表

    编写一个程序,找到两个单链表相交的起始节点. 例如,下面的两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始相交. 注意: 如果两个 ...

  10. 爬虫--selenium

    什么是selenium? 基本使用 from selenium import webdriver from selenium.webdriver.common.by import By from se ...