Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- using namespace std;
- ifstream f("input.in");
- int ma[100][100],n,m,viz[100],st[100],vf,nrc;
- void citire(){
- f>>n>>m;
- int i,x,y;
- for(i=1;i<=m;i++){
- f>>x>>y;
- ma[x][y]=ma[y][x]=1;
- }
- f.close();
- }
- void dfs(int x,int nr){
- int i,y;
- vf=1;
- st[vf]=x;
- viz[x]=nr;
- while(vf){
- y=st[vf];//y nod curent
- for(i=1;i<=n;i++)if(ma[y][i]&&viz[i]==0){
- viz[i]=nr;
- st[++vf]=i;
- break;
- }
- if(i>n)--vf;
- }
- }
- void rezolvare(){
- int x,nr,gasit,i;
- x=1;
- nr=1;
- do{
- gasit=0;
- dfs(x,nr);
- for(i=1;i<=n;i++)if(viz[i]==0){
- x=i;
- gasit = 1;
- ++nr;
- break;
- }
- }while(gasit);
- cout<<nr<<" componente conexe";
- }
- int main(){
- citire();
- rezolvare();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement