Description
解決圖論問題,首先就要思考用什麼樣的方式存儲圖。但是小鑫卻怎麼也弄不明白如何存圖纔能有利於解決問題。你能幫他解決這個問題麼?
Input
多組輸入,到文件結尾。
每一組第一行有兩個數n、m表示n個點,m條有向邊。接下來有m行,每行兩個數u、v代表u到v有一條有向邊。第m+2行有一個數q代表詢問次數,接下來q行每行有一個詢問,輸入兩個數爲a,b。
注意:點的編號爲0~n-1,2<=n<=5000 ,n*(n-1)/2<=m<=n*(n-1),0<=q<=1000000,a!=b,輸入保證沒有自環和重邊
Output
對於每一條詢問,輸出一行。若a到b可以直接連通輸出Yes,否則輸出No。
Sample
Input
2 1
0 1
2
0 1
1 0
Output
Yes
No
一:
#include<bits/stdc++.h>
using namespace std;
#define N 5050
bool s[N][N];
int main()
{
std::ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m)
{
memset(s,0,sizeof(s));
while(m--)
{
int u,v;
cin>>u>>v;
s[u][v]=1;
}
int q;
cin>>q;
while(q--)
{
int a,b;
cin>>a>>b;
if(s[a][b]==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
return 0;
}
二:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
struct node
{
int u,v,l;
}tu[500000];
int main()
{
int n,m,a,b,p,x,y;
while(~scanf("%d %d",&n,&m))
{
for(int i=0;i<m;i++)
{
scanf("%d %d",&tu[i].u,&tu[i].v);
tu[i].l = 1;
}
scanf("%d",&p);
while(p--)
{
scanf("%d %d",&x,&y);
int flag = 0;
for(int i=0;i<m;i++)
{
if(tu[i].u == x&&tu[i].v==y&&tu[i].l == 1)
{
printf("Yes\n");
flag = 1;
}
}
if(flag == 0)
printf("No\n");
}
}
return 0;
}
三:
#include<bits/stdc++.h>
using namespace std;
struct node{
int u,v,w;
}p[500001];
int cmp(node a,node b)
{
if(a.w<b.w)
return 1;
else if(a.w==b.w)
{
if(a.u<b.u)
return 1;
if(a.u==b.u)
{
if(a.v<b.v)
return 1;
else
return 0;
}
else
return 0;
}
else
return 0;
}
int main()
{
int n,m,i,q,x;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i<m;i++)
scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);
sort(p,p+m,cmp);
scanf("%d",&q);
while(q--)
{
scanf("%d",&x);
printf("%d %d\n",p[x].u,p[x].v);
}
}
return 0;
}