网络寻路(dfs或者枚举)
来源:互联网 发布:java中合法关键字 编辑:程序博客网 时间:2024/05/16 15:47
这个题一开始用了dfs,因为蓝桥杯的数据太水了,然后就过了。但是之前做过类似的题,用枚举来做的,然后就是可以避免超时
//dfs
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#include <set>#include <map> #include <sstream>using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */struct node{ int to,pre;};int h=0,ans=0;node e[200010];int head[10005];void add(int a,int b){ e[h].to=b;e[h].pre=head[a];head[a]=h; h++;}int vis[10005];int start=0;void dfs(int bg,int time1){ if(time1==3){ ans++; return ; } for(int i=head[bg];i>-1;i=e[i].pre) { node t=e[i]; if(time1==2&&t.to==start) { ans++; continue;} if(!vis[t.to]) { vis[t.to]=1; dfs(t.to,time1+1); vis[t.to]=0; } }}int main(int argc, char *argv[]) { int n,m; scanf("%d %d",&n,&m); memset(head,-1,sizeof(head)); for(int i=0;i<m;i++) { int a,b; scanf("%d %d",&a,&b); add(a,b); add(b,a); } for(int i=1;i<=n;i++) { start=i; memset(vis,0,sizeof(vis)); vis[i]=1; dfs(i,0); } printf("%d\n",ans); return 0;}
//枚举
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#include <set>#include <map> #include <sstream>using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */struct node{ int to,pre;};set<pair<int ,int > > t;int h=0,ans=0;node e[200010];int head[10005];void add(int a,int b){ e[h].to=b;e[h].pre=head[a];head[a]=h; h++;}int start=0;int main(int argc, char *argv[]) { int n,m; scanf("%d %d",&n,&m); memset(head,-1,sizeof(head)); for(int i=0;i<m;i++) { int a,b; scanf("%d %d",&a,&b); add(a,b); add(b,a); t.insert(pair<int ,int > (a,b)); } int sum=0; set<pair<int ,int > > ::iterator it; for(it=t.begin();it!=t.end();it++) { pair< int,int > temp=*it; int a=temp.first,b=temp.second;//这个.first不要记错啦啦啦 int ans1=0,ans2=0; for(int i=head[a];i>-1;i=e[i].pre) if(e[i].to!=b) ans1++; for(int i=head[b];i>-1;i=e[i].pre) if(e[i].to!=a) ans2++; sum+=ans1*ans2; } printf("%d\n",sum*2); return 0;}
0 0
- 网络寻路(dfs或者枚举)
- 历届试题网络寻路(dfs)
- poj 2718 dfs搜索或者枚举全排列
- POJ 2965(DFS+枚举)
- Flip Game(DFS枚举)
- 蓝桥网 带分数(枚举/dfs)
- poj2245枚举排列(DFS)
- hdu4574(dfs+枚举)
- 蓝桥杯——网络寻路(dfs)
- 蓝桥杯 历届试题 网络寻路 (dfs)
- DFS 枚举
- 历届试题 网络寻路 (树上dfs)
- 历届试题 网络寻路 dfs
- poj_1166_枚举(或者递归/回溯)
- hdu 4993(简单枚举或者hash)
- Fliptile (反转模拟或者dfs)
- 美团CODEM 最长树链 枚举质因数,DFS或者直接BFS
- poj Flip Game(DFS)(枚举)
- QTcpSocket 及 TCP粘包分析
- Windows多媒体定时器(精确到1ms)
- 图 基本概念和性质
- JSP三种页面跳转方式的比较
- C#中使用MATLAB
- 网络寻路(dfs或者枚举)
- linux自旋锁——读写锁
- nginx.conf配置示例讲解
- java一些常用代码的分享
- unity3D EasyAR 识别模型抖动解决方案
- Install Hadoop on local machine
- 设计模式(三)装饰者模式之java.io
- 爬取豆瓣电影数据
- 继承