美文网首页
DFS风险系数

DFS风险系数

作者: ClockworkTree | 来源:发表于2017-09-09 16:49 被阅读0次

![Uploading Paste_Image_193769.png . . .]

#include<iostream>
#include<algorithm>
#include<string>
#include<cctype>
#include<sstream>
#include<stack>
#include<cstdio>
#include<vector>
#include<fstream>
#include<ctime>
#include<map>
#include<queue> 
#include<cmath>
#define ALL(x) x.begin(),x.end()
#define ALLRE(x) x.rbegin(),x.rend()
using namespace std;

vector< vector<int> >  edge;
vector<int> path;
int u, v;
map<int, int> map1;
int count1 = 0;

void f2()
{
    for (int i = 1; i != path.size(); ++i) {
        if (i != v&&i != u&&path[i] == 1) {
            ++map1[i];
        }
    }
    return;
}

void dfs(int vertex)
{
    if (vertex == v) {
        f2();
        ++count1;
        return;         
    }
    for (int i = 0; i != edge.size(); ++i) {
        if (edge[i][0] == vertex&&path[edge[i][1]]==0) {
            path[edge[i][1]] = 1;
            dfs(edge[i][1]);
            path[edge[i][1]] = 0;
        }
        else  if (edge[i][1] == vertex&&path[edge[i][0]] == 0) {
            path[edge[i][0]] = 1;
            dfs(edge[i][0]);
            path[edge[i][0]] = 0;
        }
    }
    return;
}

int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i != m+1; ++i)
    {
        path.push_back(0);
    }
    
    for (int i = 0; i != n-1; ++i)
    {
        cin >> u >> v;
        vector<int> temp;
        temp.push_back(u);
        temp.push_back(v);      
        edge.push_back(temp);
    }
    cin >> u >> v;
    path[u] = 1;

    dfs(u);

    if (count1 == 0) cout << -1;
    else {
        int risk = 0;
        for (map<int, int>::iterator it = map1.begin(); it != map1.end(); ++it) {
            if ((*it).second == count1) ++risk;
        }
        cout << risk;
    }

    return 0;

}

/*
7 6
1 3
2 3
3 4
3 5
4 5
5 6
1 6

*/

相关文章

  • DFS风险系数

    ![Uploading Paste_Image_193769.png . . .]

  • 各种DFS

    DFS邻接矩阵遍历图 DFS邻接表遍历图 DFS回溯(不走重复路径) DFS背包(可重复选) DFS背包(不可重复选)

  • HDFS shell操作

    创建目录hdfs dfs -mkdir 查看所有目录hdfs dfs -ls / 上传文件hdfs dfs -pu...

  • 每天行动并没有你想象的难

    每天保持高速巡航状态并没有我们想象的那么辛苦,飞机在起飞和降落时的风险系数最高,飞行时的风险系数反而最小,在持续...

  • Vivi_day7亲爱的卧底经济学家11-15

    11、当风险系数为多大的时候你愿意不戴避孕套? 20%、10%?事实上,只要风险系数不是零,不良后果都是你承受不起...

  • Binary Tree(2)

    BFS vs DFS for Binary Tree What are BFS and DFS for Binar...

  • Clone Graph (Leetcode 133)

    DFS Approach: 注意,对于DFS,对map的赋值要在DFS loop开始以前。这样可以避免由于grap...

  • hdfs的命令行使用

    语法:hdfs dfs 参数 hdfs dfs -ls / 查看根路径下面的文件或文件夹 hdfs dfs -mk...

  • DFS与N皇后问题

    DFS与N皇后问题 DFS 什么是DFS DFS是指深度优先遍历也叫深度优先搜索。 它是一种用来遍历或搜索树和图数...

  • DFS及其应用

    内容概要: DFS类的实现 DFS求解连通分量 DFS求解点对之间的一个路径 DFS判定无环图和二分图 相关概念 ...

网友评论

      本文标题:DFS风险系数

      本文链接:https://www.haomeiwen.com/subject/hcreottx.html