|
|
back to boardTL#10 on C++ => Runtime Error #12 on Python3, pls help thnx Edited by author 14.08.2017 15:51 Re: TL#10 on C++ => Runtime Error #12 on Python3, pls help Count the island, cout island-1. That is right. Try use structure and dont use only letters for variables. struct Graph { vector <vector <int>> adj; // list of Adjacency vector <bool> visitado; // vertex visited int nodos,aristas,temp; // edge, var temp void leer() { cin >> nodos >> aristas >> temp; //just ignore the bridges adj = vector <vector<int>> (nodos+1); //nodos=edge visitado = vector <bool> (nodos+1,false); //visitado=visited (spanish code :p) int desde,hasta; //from, to for(int c=0;c<aristas;c++) //here create a list od adjacency { cin >> desde >> hasta; adj[desde].push_back(hasta); adj[hasta].push_back(desde); } } void start() { int contador=0; for(int c=1; c<=nodos; c++) //this bucle is for count the island { if(visitado[c]==false) { dfs(c); contador++; } } cout << contador-1 << endl; } void dfs(int nodo) // we need only one parameter in dfs function { visitado[nodo]=true; for(int c=0;c<adj[nodo].size();c++) { int vecino=adj[nodo][c]; if(visitado[vecino]==false) { dfs(vecino); } } } }; int main(){ Graph g; g.leer(); //call read function g.start(); //start for first time dfs from edge 1 to edge N } that is my code Re: TL#10 on C++ => Runtime Error #12 on Python3, pls help /*That is your code, i've fixed. Try to use struct for the next time! I really commend you that.*/ #include <bits/stdc++.h> using namespace std; void dfs(vector<bool> &b, vector<vector<int> > &a, int &i, vector<int> &size) //the problem in your code is here, &b, &a, &i, &size, just edit that and have a nice day { b[i]=true; int j,s=size[i]; for (j = 0;j < s;j++) { if (b[a[i][j]]==false){ dfs(b,a,a[i][j],size);} } } int main(){ int n,t,m; cin>>n>>t>>m; vector<vector<int> > a(n,vector<int>()); vector<int> size(n,0); int x,y,i; for (i = 0;i < t;i++) { cin>>x>>y; a[x-1].push_back(y-1); size[x-1]++; size[y-1]++; a[y-1].push_back(x-1); } int c=0; vector<bool> b(n,false); for (i = 0;i < n;i++) { if (b[i]==false){ dfs(b,a,i,size); c++;} } cout<<c-1; } Re: TL#10 on C++ => Runtime Error #12 on Python3, pls help Muchas gracias |
|
|