zoukankan      html  css  js  c++  java
  • cf780E(dfs)

    题目链接: http://codeforces.com/problemset/problem/780/E

    题意: 给出一个 n 个点 m 条边的图, 有 k 个人, 初始位置可以为任意位置, 每个人最多不能经过超过 ceil(2 * n / k) 个顶点. 要使 k 个人经历所有顶点, 并输出 k 个人经历的顶点数目和路径.

    思路: 先 dfs 遍历一下图, 并记录路径 (回溯路径也要记录). 然后再将路径按条件分配给 k 个人即可.

    代码:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <vector>
     4 using namespace std;
     5 
     6 const int MAXN = 2e5 + 10;
     7 vector<int> vt[MAXN];
     8 int vis[MAXN], path[MAXN << 2];
     9 int indx = 0;
    10 
    11 void dfs(int x){
    12     vis[x] = 1;
    13     path[++indx] = x;
    14     for(int i = 0; i < vt[x].size(); i++){
    15         if(!vis[vt[x][i]]){
    16             dfs(vt[x][i]);
    17             path[++indx] = x;
    18         }
    19     }
    20 }
    21 
    22 int main(void){
    23     int n, m, k, x, y;
    24     scanf("%d%d%d", &n, &m, &k);
    25     for(int i = 0; i < m; i++){
    26         scanf("%d%d", &x, &y);
    27         vt[x].push_back(y);
    28         vt[y].push_back(x);
    29 
    30     }
    31     dfs(1);
    32     int cnt = (2 * n + k - 1) / k;
    33     for(int i = 0; i < k; i++){
    34         int cc = min(cnt, indx);
    35         if(!cc){
    36             printf("1 1
    ");
    37             continue;
    38         }
    39         printf("%d ", cc);
    40         for(int j = 0; j < cc && indx; j++,indx--){
    41             printf("%d ", path[indx]);
    42         }
    43         puts("");
    44     }
    45     return 0;
    46 }
    View Code
  • 相关阅读:
    html5 自定义属性data-*
    企业微信接口授权
    js对象---字符串
    谈谈html5新增的元素及其他功能
    模拟缓存
    jdbc数据库连接
    面向对象的理解
    最简单的Spring+SpringMVC+Mybatis的整合
    EF报错 附加类型model失败
    c# Web服务远程“调用”调试
  • 原文地址:https://www.cnblogs.com/geloutingyu/p/7162035.html
Copyright © 2011-2022 走看看