F - Berland and the Shortest Paths

思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int> using namespace std; const int N = 5e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = ; int n, m, k, tot, d[N], head[N], ans[N];
LL cnt;
struct node {
int u, v, nx;
} edge[N]; void add(int u, int v) {
edge[tot].u = u;
edge[tot].v = v;
edge[tot].nx = head[u];
head[u] = tot++;
} vector<int> vec[N]; void bfs() {
queue<int> que;
d[] = ;
que.push(); while(!que.empty()) {
int u = que.front(); que.pop();
for(int i = head[u]; ~i; i = edge[i].nx) {
int v = edge[i].v;
if(d[v] != -) continue;
d[v] = d[u] + ;
que.push(v);
}
}
} void dfs(int pos) {
if(cnt <= ) return;
if(pos == n + ) {
cnt--;
for(int i = ; i <= m; i++)
printf("%d", ans[i]);
puts("");
return;
}
for(int i = ; i < vec[pos].size(); i++) {
ans[vec[pos][i]] = ;
dfs(pos + );
ans[vec[pos][i]] = ;
}
}
int main(){
memset(head, -, sizeof(head));
scanf("%d%d%d", &n, &m, &k);
for(int i = ; i <= m; i++) {
int u, v; scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
} memset(d, -, sizeof(d)); bfs();
for(int i = ; i < tot; i++) {
int u = edge[i].u, v = edge[i].v;
if(d[u] + == d[v]) {
vec[v].push_back(i / + );
}
} cnt = ;
for(int i = ; i <= n; i++) {
cnt *= (int) vec[i].size();
if(cnt >= k) break;
} cnt = min(cnt, 1ll * k);
printf("%lld\n", cnt);
dfs();
return ;
} /*
*/

Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths的更多相关文章

  1. Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths

            F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 ...

  2. Codeforces Round #617 (Div. 3)F. Berland Beauty

    题意: 给一棵树,边权未知,现在给m组约束,每组约束给出从u到v路径中的最小值,现在让你给出一组边权,使得符合之前的约束,不能给出输出-1 思路: 因为n较小,对于每组约束我们可以直接暴力修改路径上的 ...

  3. Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths (二分图染色)

    题意:有\(n\)个点和\(m\)条无向边,现在让你给你这\(m\)条边赋方向,但是要满足任意一条边的路径都不能大于\(1\),问是否有满足条件的构造方向,如果有,输出一个二进制串,表示所给的边的方向 ...

  4. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  5. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  6. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  7. 构造 Codeforces Round #Pi (Div. 2) B. Berland National Library

    题目传送门 /* 题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆 构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况 ...

  8. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  9. Codeforces 1005 F - Berland and the Shortest Paths

    F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所 ...

随机推荐

  1. win32/linux 线程 log

    原文 #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32 #includ ...

  2. (转)C++常见问题: 字符串分割函数 split

    http://www.cnblogs.com/dfcao/p/cpp-FAQ-split.html C++标准库里面没有字符分割函数split ,这可太不方便了,我已经遇到>3次如何对字符串快速 ...

  3. 目前最快速的多线程Kmeans算法,java实现

    目前最快速Kmeans算法,并由java实现!面对很大的K值表现依然很好. 代码地址: https://github.com/Jethu1/fastKmeans #1.这是一个由java实现的的,多线 ...

  4. Linux系统中各目录的作用

    /binbin是binary的缩写.这个目录沿袭了UNIX系统的结构,存放着使用者最经常使用的命令.例如cp.ls.cat,等等. /boot这里存放的是启动Linux时使用的一些核心文件. /dev ...

  5. jQuery各版本CDN

    jquery-2.1.1 注:jquery-2.0以上版本不再支持IE 6/7/8)百度引用地址 (推荐目前最稳定的,不会出现延时打不开情况) 百度压缩版引用地址:<script src=”ht ...

  6. IDEA 启动时,报“淇℃伅”的字符

    IDEA 启动时,报“淇℃伅”的字符,如下: 解决办法: 修改tomcat安装目录下的config/logging.properties文件,找到java.util.logging.ConsoleHa ...

  7. 查询timestamp类型数据

    $where=" roleid = 8 and lizhi = 0 and branchid IN (".implode(",",$ids).") a ...

  8. Go语言 7 并发编程

    文章由作者马志国在博客园的原创,若转载请于明显处标记出处:http://www.cnblogs.com/mazg/ Go学习群:415660935 今天我们学习Go语言编程的第七章,并发编程.语言级别 ...

  9. King's Quest POJ - 1904 匈牙利算法的思想+tarjan缩点+染色

    题目链接:https://cn.vjudge.net/problem/POJ-1904 自己一开始的想法,打算用匈牙利算法实现,找二分图的最大匹配.但是打了打发现,不太好实现.原因如下:匈牙利算法是不 ...

  10. tensorflow.nn.bidirectional_dynamic_rnn()函数的用法

    在分析Attention-over-attention源码过程中,对于tensorflow.nn.bidirectional_dynamic_rnn()函数的总结: 首先来看一下,函数: def bi ...