并查集的路径压缩。

 #include <stdio.h>
#include <string.h> #define MAXNUM 100005 int deg[MAXNUM], bin[MAXNUM];
char isCycle[MAXNUM]; int find(int x) {
int r = x;
int i = x, j; while (r != bin[r])
r = bin[r]; while (i != r) {
j = bin[i];
bin[i] = r;
i = j;
} return r;
} void merge(int x, int y) {
int fx;
int fy; fx = find(x);
fy = find(y);
if (fx != fy)
bin[fx] = fy;
} int main() {
int n, m;
int i, x, y; while (scanf("%d %d", &n, &m)!=EOF && (n||m)) {
for (i=; i<n; ++i)
bin[i] = i;
memset(isCycle, , sizeof(isCycle));
memset(deg, , sizeof(deg));
while (m--) {
scanf("%d %d", &x, &y);
deg[x]++;
deg[y]++;
merge(x, y);
}
m = ;
for (i=; i<n; ++i) {
x = find(i);
if (isCycle[x] == ) {
++m;
isCycle[x] = ;
}
}
for (i=; i<n; ++i)
if (deg[i] != )
isCycle[bin[i]] = ;
x = ;
for (i=; i<n; ++i)
if (isCycle[i])
++x;
printf("%d %d\n", m, x);
} return ;
}

【HDOJ】3560 Graph’s Cycle Component的更多相关文章

  1. 【HDOJ】3726 Graph and Queries

    Treap的基础题目,Treap是个挺不错的数据结构. /* */ #include <iostream> #include <string> #include <map ...

  2. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...

  3. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  4. 【HDOJ】1706 The diameter of graph

    这么个简单的题目居然没有人题解.floyd中计算数目,同时注意重边. /* 1706 */ #include <iostream> #include <string> #inc ...

  5. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  6. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  7. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  8. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  9. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

随机推荐

  1. MarkDown 格式生产类型

    -- 不默认换行, 真的结束, 包括格式设定,记得空一行. -- 学习参考地址如下, 讲的不好, 太复杂, 不适合新手. 有好读的更好. ** 但是江湖规矩 还是引用下吧 这是地址(http://wo ...

  2. ECMAScript6入门系列一

    let 命令 { let a = 10; var b = 1; } a // ReferenceError: a is not defined. b // 1 [注]:let与var相似,用来声明变量 ...

  3. 如何在Angular2中使用Forms

    在Angular2中形成两个基本对象是Control和ControlGroup. 控制用户输入 Control 一个控制表示一个输入字段(ngFormControl) 一个控制可以绑定到一个input ...

  4. 10.12_win8风格,把专业书籍当小说看,SQLite

    (1)CSS:Metro UI CSS(Metroui) 是一套实现了 Windows 8 的 Metro 风格界面的 CSS 框架..官网.BootMetro . (2)[开源访谈]CecOS作者 ...

  5. MongoDB源码分析——mongo与JavaScript交互

    mongo与JavaScript交互 源码版本为MongoDB 2.6分支     之前已经说过mongo是MongoDB提供的一个执行JavaScript脚本的客户端工具,执行js其实就是一个js和 ...

  6. ZEngine游戏框架需求稿

    早在2013就开始接触cocos2dx游戏框架,觉得的使用框架对于游戏开发者来说有些不方便.我便想在cocos2dx的框架基础之上实现ZEngine库,方便开发者编辑动画,以及着重于对游戏逻辑的处理. ...

  7. Headfirst设计模式的C++实现——命令模式(Command)

    先看如果不用命令模式的实现: light.h #ifndef _LIGHT_H_ #define _LIGHT_H #include <iostream> class LIGHT { pu ...

  8. Mysql DOC阅读笔记

    Mysql DOC阅读笔记 转自我的Github Speed of SELECT Statements 合理利用索引 隔离调试查询中花费高的部分,例如函数调用是在结果集中的行执行还是全表中的行执行 最 ...

  9. jQuery学习 day01

    最近受某大牛指点(我不会说他姓范),了解了一下jQuery,据说很牛X,就了解了一下,第一天,分享给大家一些心得吧. 1.首先就是导入jQuery文件了,这里我是去jQuery官网下载的.(大家可以去 ...

  10. sonar-maven-plugin错误2

    From maven-sonar-plugin 2.7, SonarQube < 4.5 is no longer supported. If using SonarQube instance ...