Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define LL long long
- #define N ((int)1e5 + 8)
- #define MAX ((int)1e9 + 8)
- #define fastio ios_base::sync_with_stdio(false),cin.tie(NULL)
- using namespace std;
- vector < int > edge[N]; /// edge[i] = list of adjacent nodes of node 'i'
- bool vis[N];
- /// finding component
- void dfs(int node, vector < int >& comp)
- {
- comp.push_back(node);
- vis[node] = 1;
- for(int a:edge[node]){
- if(vis[a] == 0) dfs(a);
- }
- } /// O ( n + m )
- vector < int > comp[N];
- /// finding cycle:
- int par[N];
- void dfs(int node)
- {
- vis[node] = 1;
- for(int a:edge[node]){
- if(vis[a] == 0){
- dep[a] = dep[node] + 1;
- par[a] = node;
- dfs(a);
- }
- else if(dep[a] < dep[node] - 1){
- /// cycle found;
- int cur = node;
- while(cur != a){
- cout<<cur<<" ";
- cur = par[cur];
- }
- cout<<a<<endl;
- }
- }
- }
- void bfs(int source)
- {
- memset(vis,0,sizeof vis);
- queue < int > que;
- que.push(source);
- vis[source] = 1;
- dis[source] = 0;
- while(!que.empty()){
- int node = que.front();
- que.pop();
- for(int a:edge[node]){
- if(vis[a] == 0){
- que.push(a);
- vis[a] = 1;
- dis[a] = dis[node] + 1;
- }
- }
- } /// O (n + m)
- }
- int dxx[] = {0 , 1, 0, -1}; /// change in row
- int dyy[] = {-1 , 0, 1, 0}; /// change in col
- /// +dxx[0], +dyy[0] => left
- /// +dxx[1] , +dyy[1] => down
- bool isValid(int row , int col)
- {
- if(min(row , col) < 0) return 0;
- if(row >= n || col >= m) return 0;
- if(grid[row][col] == '#' || grid[row][col] == 'm') return 0;
- return 1;
- }
- void dfs(int row , int col) /// {row,col} => node
- {
- for(int i = 0 ; i < 4 ; i++){
- int x = row + dxx[i] , y = col = dyy[i];
- if(isValid(x,y) && vis[x][y] == 0) dfs(x , y);
- }
- }
- void bfs(int row , int col)
- {
- queue < pair < int , int > > que;
- que.push({row , col});
- vis[row][col] = 1;
- dis[row][col] = 0;
- while(!que.empty()){
- int row = que.front().first , col = que.front().second;
- que.pop();
- for(int i = 0 ; i < 4 ; i++){
- int x = row + dxx[i] , y = col = dyy[i];
- if(isValid(x,y) && vis[x][y] == 0){
- dis[x][y] = dis[row][col] + 1;
- vis[x][y] = 1;
- que.push({x,y});
- }
- }
- } /// O (n + m)
- }
- int main()
- {
- int n , m;
- cin>>n>>m; /// n = nodes , m = edges
- while(m--){
- int a, b; /// undirected
- cin>>a>>b; /// a --> b , b --> a
- edge[a].push_back(b);
- edge[b].push_back(a);
- }
- for(int node = 1 ; node <= n ; node++){ /// O(n)
- for(int i:edge[node]){ /// O(sizeOf(edge[node]))
- /// code
- }
- } /// O(m)
- for(int node = 1 ; node <= n ; node++){ /// O(n)
- for(int i = 1 ; i <= n ; i++){ /// O(n)
- if(edge[node][i] == 1){
- /// code
- }
- }
- } /// O(n*n)
- /// finding connected components
- int tot = 0;
- for(int i = 1 ; i <= n ; i++){
- if(vis[i] == 0){
- dfs(i , comp[tot]);
- tot++;
- }
- }
- cout<<tot<<endl;
- for(int i = 0 ; i < tot; i++){
- for(int a:comp[i]){
- /// code;
- }
- }
- /// finding cycle
- dep[1] = 0;
- par[1] = 0;
- dfs(1);
- BuildGraph();
- while(t--){
- int src , des;
- bfs(src);
- if(vis[des] == 0) cout<<"impossible\n";
- else cout<<dis[des]<<endl;
- }
- }
- void BuildGraph()
- {
- vector < int > prime = Seive(1000);
- for(int i = 1 ; i <= 1000 ; i++){
- for(int p:prime){
- if(p >= i) break;
- if(i % p == 0 && i+p <= 1000){
- edge[i].push_back(i+p);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment