CP003133 邻接矩阵题解
2023-10-04 19:49:14
发布于:江苏
36阅读
0回复
0点赞
CP003133 邻接矩阵题解
这题考验图的存储,图的存储方式有:
- 邻接矩阵
- 邻接表
这里介绍邻接矩阵。其原理是:
若从节点A可通到节点B,则二维数组mp[A,B]=1。
#include<bits/stdc++.h>
#define io freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout)
#define ll long long
#define ull unsigned long long
using namespace std;
// variables setting
int mp[1005][1005];
int m,n,q;
// functions(others) defining
void loadRoad()
{
int x,y;
for(int i=1;i<=m;i++)
{
cin>>x>>y;
mp[x][y]=1;
mp[y][x]=1;//因为是无向图,所以互可到达
}
}
void loadAdvice()
{
int x,y;
for(int i=1;i<=q;i++)
{
cin>>x>>y;
if(mp[x][y]||mp[y][x])puts("Cancel");
else puts("Accepted");
}
}
// the program subject
void program()
{
cin>>n>>m>>q;
loadRoad();
loadAdvice();
}
// the main function
int main()
{
//io;
program();
return 0;
}
这里空空如也
有帮助,赞一个