用bitset
#define IOS ios::sync_with_stdio(false) const int MAX=4e4+40; using namespace std; bitset<MAX>fri[MAX]; int main() { // freopen("out.txt","w",stdout); IOS;cin.tie(0); int n,m; cin>>n>>m; int i,j,k; //for(i=0;i<MAX;i++) // fri[i].set(); //for(i=0;i<MAX;i++) // fri[i].flip(); for(i=0;i<m;i++){ int u,v; cin>>u>>v; fri[u][v]=1; fri[v][u]=1; } int q;cin>>q; while(q--){ int s,t;cin>>s>>t; cout<<(fri[s]&fri[t]).count()<<endl; } }
#include <iostream> #include <bitset> using namespace std; static const int maxn = 4e4+10; int main(void) { bitset<maxn> g[maxn]; //graph; int n,m,q,u,v; scanf("%d %d",&n,&m); for(int i = 0;i < m;i++) { scanf("%d %d",&u,&v); g[u][v] = g[v][u] = 1; } scanf("%d",&q); for(int i = 0;i < q;i++) { scanf("%d %d",&u,&v); cout<<(g[u]&g[v]).count()<<endl; } return 0; }
+1
用bitset
include
+1