eighth!

Problem Description

解决图论问题,首先就要思考用什么样的方式存储图。但是小鑫却怎么也弄不明白如何存图才能有利于解决问题。你能帮他解决这个问题么?

Input
 多组输入,到文件结尾。

每一组第一行有两个数nm表示n个点,m条有向边。接下来有m行,每行两个数uv代表uv有一条有向边。第m+2行有一个数q代表询问次数,接下来q行每行有一个询问,输入两个数为ab

注意:点的编号为0~n-12<=n<=5000 n*(n-1)/2<=m<=n*(n-1)0<=q<=1000000a!=b,输入保证没有自环和重边

Output
 对于每一条询问,输出一行。若ab可以直接连通输出Yes,否则输出No
Example Input
2 1
0 1
2
0 1
1 0
Example Output
Yes
No

code1://临阶矩阵

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
bool s[5000][5000];
int main()
{
    int q, n, m, u, v, a, b, i, j;
    while(~scanf("%d%d", &n, &m))
    {
        memset(s, 0, sizeof(s));
        for(i = 0; i<m; i++)
        {
            scanf("%d%d", &u, &v);
            s[u][v] = 1;
        }
        scanf("%d", &q);
        for(j = 0; j<q; j++)
        {
            scanf("%d%d", &a, &b);
            if(s[a][b]==1) printf("Yes\n");
            else printf("No\n");
        }
    }
    return 0;
}
code2://临阶链表

#include<iostream>
#include<cstring>
#include<stdlib.h>
using namespace std;
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int n, m, i, u, v;
    struct node *s[500050], *p, *q;
    while(cin>>n>>m)
    {
        for(i = 0;i<n;i++)
        {
            s[i] = NULL;
        }
        for(i = 0;i<m;i++)
        {
            cin>>u>>v;
            if(s[u]==NULL)
            {
                s[u] = (struct node*)malloc(sizeof(struct node));
                s[u]->data = v;
                s[u]->next = NULL;
            }
            else
            {
                q = s[u]->next;
                p = (struct node*)malloc(sizeof(struct node));
                p->data = v;
                p->next = q;
                s[u]->next = p;
            }
        }
        int qq;
        cin>>qq;
        while(qq--)
        {
            int a, b, flag = 0;
            cin>>a>>b;
            if(s[a] == NULL) cout<<"No"<<endl;
            else
            {
                p = s[a];
                while(p)
                {
                    if(p->data == b)
                    {
                        flag = 1;
                        break;
                    }
                    p = p->next;
                }
                if(flag) cout<<"Yes"<<endl;
                else cout<<"No"<<endl;
            }
        }
    }
}
#include <bits/stdc++.h>


struct node
{
    int u,v,w;
} s[500010];
int cmp(struct node a,struct node b)
{
    if(a.w!=b.w)
        return a.w<b.w;
    else if(a.u!=b.u)
        return a.u<b.u;
    else
        return a.v<b.v;
}
using namespace std;
int main()
{
    int i,n,k,m,t,l1,l2,l3;
    while(cin>>n>>m)
    {
        for(i=0; i<m; i++)
            s[i].w=2147483647;
        for(i=0; i<m; i++)
        {
            cin>>l1>>l2>>l3;
            s[i].u=l1;
            s[i].v=l2;
            s[i].w=l3;
        }
        sort(s,s+m,cmp);
        cin>>k;
        while(k--)
        {
            cin>>t;
            cout<<s[t].u<<" "<<s[t].v<<endl;
        }
    }
        return 0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值