南昌邀请赛 Distance on the tree(邻接表建立模板)
DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(National Olympiad in Informatics in Provinces) in Senior High School. So when in Data Structure Class in College, he is always absent-minded about what the teacher says.
The experienced and knowledgeable teacher had known about him even before the first class. However, she didn't wish an informatics genius would destroy himself with idleness. After she knew that he was so interested in ACM(ACM International Collegiate Programming Contest), she finally made a plan to teach him to work hard in class, for knowledge is infinite.
This day, the teacher teaches about trees." A tree with nn nodes, can be defined as a graph with only one connected component and no cycle. So it has exactly n-1n−1 edges..." DSM is nearly asleep until he is questioned by teacher. " I have known you are called Data Structure Master in Graph Theory, so here is a problem. "" A tree with nn nodes, which is numbered from 11 to nn. Edge between each two adjacent vertexes uu and vv has a value w, you're asked to answer the number of edge whose value is no more than kk during the path between uu and vv."" If you can't solve the problem during the break, we will call you DaShaMao(Foolish Idiot) later on."
The problem seems quite easy for DSM. However, it can hardly be solved in a break. It's such a disgrace if DSM can't solve the problem. So during the break, he telephones you just for help. Can you save him for his dignity?
Input
In the first line there are two integers n,mn,m, represent the number of vertexes on the tree and queries(2 \le n \le 10^5,1 \le m \le 10^52≤n≤105,1≤m≤105)
The next n-1n−1 lines, each line contains three integers u,v,wu,v,w, indicates there is an undirected edge between nodes uu and vv with value ww. (1 \le u,v \le n,1 \le w \le 10^91≤u,v≤n,1≤w≤109)
The next mm lines, each line contains three integers u,v,ku,v,k , be consistent with the problem given by the teacher above. (1 \le u,v \le n,0 \le k \le 10^9)(1≤u,v≤n,0≤k≤109)
Output
For each query, just print a single line contains the number of edges which meet the condition.
样例输入1复制
3 3 1 3 2 2 3 7 1 3 0 1 2 4 1 2 7
样例输出1复制
0 1 2
样例输入2复制
5 2 1 2 1000000000 1 3 1000000000 2 4 1000000000 3 5 1000000000 2 3 1000000000 4 5 1000000000
样例输出2复制
2 4
题意:
n,m表示n个顶点m条询问然后是n-1行表示n-1条边和m条询问
每个询问包含u,v,k表示从u到v的路径上有多少条边的值不超过k
所构图是个树 没有环
解题思路:
超时了,留坑。。
我的想法有点简单,直接邻接表存图然后bfs,如果路径上有符合条件的边就step++
代码:
#include<bits/stdc++.h>
using namespace std;
int n,m,st,en,k;
int head[100010],book[100010];
int cnt;
struct node
{
int x,step;
};
struct e
{
int u,v,w,next;
} edge[100010];
void add(int u,int v,int w)//向所要连接的表中加入边
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
node getnode(int x,int step)
{
node q;
q.x=x;
q.step=step;
return q;
}
int bfs(int st,int step)
{
queue<node> q;
node now;
q.push(getnode(st,step));
while(!q.empty())
{
now=q.front();
q.pop();
int tk=head[now.x];
if(edge[tk].u==en) return now.step;
while(book[edge[tk].v]==0&&tk!=-1)
{
book[edge[tk].v]=1;
int tstep=now.step;
if(edge[tk].w<=k)
{
tstep++;
}
q.push(getnode(edge[tk].v,tstep));
tk=edge[tk].next;
}
}
return -1;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int u,v,w;
memset(head,-1,sizeof(head));
cnt=1;
for(int i=1;i<=n-1;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
// printf("****************\n");
// for(int i=1;i<=n;i++)
// {
// int k=head[i];
// while(k!=-1)
// {
// printf("%d %d %d\n",edge[k].u,edge[k].v,edge[k].w);
// k=edge[k].next;
// }
// }
// printf("****************\n");
for(int i=1;i<=m;i++)
{
memset(book,0,sizeof(book));
scanf("%d%d%d",&st,&en,&k);
if(st==en)
{
printf("0\n");
continue;
}
int sum=bfs(st,0);
printf("%d\n",sum);
}
}
return 0;
}
邻接表建立参考:
https://blog.csdn.net/haut_ykc/article/details/52177161
/*
* 构建邻接表模板
*
*/
#include<stdio.h>
#include<string.h>
int head[100100];//表头,head[i]代表起点是i的边的编号
int cnt;//代表边的编号
struct s
{
int u;//记录边的起点
int v;//记录边的终点
int w;//记录边的权值
int next;//指向上一条边的编号
}edge[100010];
void add(int u,int v,int w)//向所要连接的表中加入边
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i;
cnt=0;
memset(head,-1,sizeof(head));//清空表头数组
for(i=0;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
int u,v,w;
scanf("%d",&u);
for(i=head[u];i!=-1;i=edge[i].next)//输出所有与起点为u相连的边的终点和权值
{
v=edge[i].v;
w=edge[i].w;
printf("%d %d\n",v,w);
}
}
return 0;
}
#include<stdio.h>
#include<vector>
using namespace std;
#define SIZE 1000
vector<vector<int> >adj(SIZE);
int n,m;
void init()
{
scanf("%d%d",&n,&m);
int i,j;
int a,b;
for(i=1;i<=n;i++)
{
adj[i].clear(); //清除
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
adj[a].push_back(b);
adj[b].push_back(a);
}
}
void print()
{
int i;
int j;
for(i=1;i<=n;i++)
{
printf("dian %d:",i);
for(j=0;j<adj[i].size();j++)
{
printf("%d ",adj[i][j]);
}
printf("\n");
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
print();
}
return 0;
}