Jump to content
a13x4nd7u

Grafuri neorientate

Recommended Posts

Posted

#include <cstdio>//sau stdio.h (BORLANDC)
#include <iostream>//sau iostream.h (BORLANDC)
#include <fstream>//sau fstream.h (BORLANDC)

using namespace std;//<--stergi daca folosesti compilatoare vechi (BORLANDC)

//n este numarul varfurilor
//in a este matricea de adiacenta
//in gr sunt gradele varfurilor
int n,a[100][100],gr[100];

int grad(void)
{
int i,j,S;
for(i=0;i<=n;i++) {
S=0;
for(j=0;j<=n;j++)
S=S+a[i][j];
gr[i]=S;
}

return 0;
}

int grad_maxim(void) {
int i,max=gr[1];

for(i=2;i<=n;i++)
if(max < gr[i])
max=gr[i];

return max;
}

int afisare(int max)
{
int i;
//afisare grade:
for(i=1;i<=n;i++)
cout<<"Gradul varfului "<<i<<" este "<<gr[i]<<endl;
//afisare grad maxim:
cout<<"Varfurile care au grad maxim sunt: ";

for(i=1;i<=n;i++)
if(gr[i] == max)
cout<<i<<" ";

cout<<endl;
return 0;
}

int golire(void)
{
int i,j;
for(i=0;i<100;i++)
for(j=1;j<100;j++)
a[i][j]=0;

return 0;
}


int citire(void)
{
int i,j;
cin>>n;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++) {
cin>>a[i][j];
}

return 0;
}

int varfuri_terminale(void)
{
//au grad 1 (au o singura legatura cu alt varf)

cout<<"Varfuri terminale: ";
int i;
for(i=1;i<=n;i++)
if(gr[i] == 1)
cout<<i<<" ";
cout<<"\n";
return 0;
}

int varfuri_conexe(void)
{
//au grad >= 2 (nu sunt nici terminale nici izolate)
cout<<"Varfuri terminale: ";
int i;
for(i=1;i<=n;i++)
if(gr[i] >= 2)
cout<<i<<" ";
cout<<"\n";
return 0;
}

int varfuri_izolate(void)
{
//au grad 0 (adica nu au nicio legatura cu alt varf)
cout<<"Varfuri izolate: ";
int i;
for(i=1;i<=n;i++)
if(gr[i] == 0)
cout<<i<<" ";
cout<<"\n";
return 0;
}

int citire_vf(int &vf)
{
//cout<<"Vf=";
cin>>vf;

if(vf > n)//varful nu poate fi mai mare decat numarul de varfuri
return -1;
return 0;
}

int vecini(int vf)
{
cout<<"Vecinii lui "<<vf<<" sunt: ";
int i;
for(i=1;i<=n;i++)
if(a[i][vf] == 1)
cout<<i<<" ";

cout<<"\n";
return 0;
}


int main(int argc,char** argv)
{
int maxim,varf;
golire();//goleste matricea

freopen("date.in","r",stdin);//deschide citirea normale (stdin) ca fisier
freopen("date.out","w",stdout);//deschide afisarii normale (stdout) ca fisier

citire();//citeste matricea
grad();//calculeaza toate gradele

citire_vf(varf);//este citit un varf oarecare
vecini(varf);//sunt gasiti vecinii varfului

maxim=grad_maxim();//gaseste gradul maxim
varfuri_terminale();//gaseste si afiseaza varfurile terminale
varfuri_conexe();//gaseste si afiseaza varfurile conexe
varfuri_izolate();//gaseste si afiseaza toate varfurile izolate

afisare(maxim);//afisare vaarfuri care au gradul maxim

return 0;
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...