1032. Sharing (25)
2015-12-20 10:16
323 查看
遍历所有地址 记录被遍历的地址次数 如有大于1的则是第一个相同首地址
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<set>
using namespace std;
#define MAXnum 100005
#define max(a,b) a>b?a:b
struct node{
int num;
char c;
int next;
}a[100001];
int main()
{
int root1,root2,n;
scanf("%d%d%d",&root1,&root2,&n);
int index;
for(int i=0;i<n;i++)
{
scanf("%d",&index);
scanf(" %c%d",&a[index].c,&a[index].next);
// printf("%d%c%d",index,a[index].c,a[index].next);
a[index].num=0;
}
int address=root1;
while(address!=-1)
{
a[address].num++;
address=a[address].next;
}
address=root2;
int flag=0;
while(address!=-1)
{
a[address].num++;
if(a[address].num>1)
{
flag=1;
break;
}
address=a[address].next;
}
if(flag)
printf("%05d\n",address);
else
printf("-1\n");
return 0;
}
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<set>
using namespace std;
#define MAXnum 100005
#define max(a,b) a>b?a:b
struct node{
int num;
char c;
int next;
}a[100001];
int main()
{
int root1,root2,n;
scanf("%d%d%d",&root1,&root2,&n);
int index;
for(int i=0;i<n;i++)
{
scanf("%d",&index);
scanf(" %c%d",&a[index].c,&a[index].next);
// printf("%d%c%d",index,a[index].c,a[index].next);
a[index].num=0;
}
int address=root1;
while(address!=-1)
{
a[address].num++;
address=a[address].next;
}
address=root2;
int flag=0;
while(address!=-1)
{
a[address].num++;
if(a[address].num>1)
{
flag=1;
break;
}
address=a[address].next;
}
if(flag)
printf("%05d\n",address);
else
printf("-1\n");
return 0;
}
相关文章推荐
- Daily Scrumming* 2015.12.19(Day 11)
- 深入分析JavaWeb Item28 -- 使用JDBC对数据库进行CRUD
- Android TextUtils的常用方法
- Java连接ftp服务器实例代码
- 防火墙查看、关闭与开启命令
- eclipse里面运行tomcat显示无法显示页面
- EHCache分布式缓存集群环境配置
- org.hibernate.InvalidMappingException: Could not parse mapping document from resource 处理办法
- _tkinter.TclError: no display name and no $DISPLAY environment variable
- 修改GitHub上项目语言显示
- VB.NET中的类和模块
- JDK小技巧
- 初识Power Builder9.0
- 1021. Couples
- win install pip
- codeforce
- inet_ntoa函数段错误(Segmentation fault)
- MapReduce的组合式,迭代式,链式
- MVC jsonModelBuilder
- 2010年提高组第三题-关押罪犯-题解