摘要:题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1645思路: 图论模型化,种类数就是点,对数就是边, 要求始终不存在子图使点数等于边数,就是不允许有环,使用并查集即可。代码:#include#include#define N 100001using namespace std;int p[N+5];int find(int x){ return p[x]==x?x:p[x]=find(p[x]);}vo
阅读全文
摘要:题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4707题目给出一颗树,要求求出深度大于D的结点的个数。有两种方法,改写dfs,给一个参数放层数(额,其实这里不需要转化为有根树,多余了)代码:#include#include#include#includeusing namespace std;#define maxn 100005vector G[maxn];int p[maxn];int lev[maxn];//vector leaf;void dfs(int u,int fa,int level) // 无根树转化为有根树{ in...
阅读全文
摘要:题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4496将题目要查询的数倒过来求,先加一条边,保存cc(连通分支数) 然后再加一条边....当合并两个等价类的时候连通分支减1代码:#include#include#include#includeusing namespace std;int p[10000];int u[100000];int v[100000];int ans[100000];int find(int x){ return p[x]==x?x:p[x]=find(p[x]);}int main(){ int n,m; ...
阅读全文
摘要:题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1233模板题,kruskal求最小生成树。 并查集是个好东西啊 就是注意一点 输入边的信息时,角标应该是从0开始的代码:#include#include#includeusing namespace std;struct edge{ int u; int v; int w;};int p[100];edge e[5000];bool cmp(edge a,edge b){ return a.w>n) { if(n==0) break; m=n*(n-1)/...
阅读全文
摘要:题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1232直接求连通分支数,有重边不影响dfs ,有n个连通分支块,就恰好最少需要n-1条边代码:#include#include#include#includeusing namespace std;vector G[1000];int maxn;bool vis[1000];void dfs(int u){ vis[u]=1; int d=G[u].size(); for(int i=0;i>n>>m) { if(n==0) break; memset(vis...
阅读全文
摘要:题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1878判断图上是否有欧拉回路,首先要求是连通图,然后每个顶点的度数必须都为偶数(如果存在两个顶点度数为奇数则是存在欧拉通路)用dfs求连通分支的个数代码:#include#include#include#includeusing namespace std;vector G[1000];bool vis[1000];void dfs(int u){ vis[u]=1; int d=G[u].size(); for(int i=0;i>n>>m) { for(int i...
阅读全文
摘要:题目地址:http://poj.org/problem?id=2769要点 :1 如果在确定了模数后再在循体里面用双重for循环检测是否存在 同余的一定会超时 需要寻找O(n)复杂度的方法 2一开始想使用map 检测是否重复,结果还是超时 最后还是用类似于筛法的方法,关键点是初始化时只能(也只需要)初始化到k,否则仍会tle#include#include#includeusing namespace std;int main(){ bool find[100000]; int size; cin>>size; int n; while(cin>>n) { map...
阅读全文