Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- using namespace std;
- struct componenta
- {
- int nod; /// nodul de val. minima al c.c;
- int nrV;
- } t[103];
- int a[103][103], n, nr_CCon;
- int viz[103];
- void creare_MatAd()
- {
- ifstream fin ("componenteconexe3.in");
- fin >> n;
- int x, y;
- while(fin >> x >> y) a[x][y] = a[y][x] = 1;
- }
- void DFS(int k)
- {
- viz[k] = nr_CCon;
- for(int i = 1; i <= n; ++i)
- if (!viz[i] && a[i][k])
- DFS(i);
- }
- /// Sortez descrescator in functie de nr. de varfuri:
- void Sortare()
- {
- for(int i = 1; i < nr_CCon; ++i)
- for(int j = i + 1; j <= nr_CCon; ++j)
- if (t[i].nrV < t[j].nrV)
- swap(t[i], t[j]);
- }
- void conex()
- {
- for(int i = 1; i <= n; ++i)
- if (!viz[i])
- {
- ++nr_CCon;
- DFS(i);
- t[nr_CCon].nod = i;
- }
- /// Determin cate varfuri are fiecare comp. conexa:
- for(int i = 1; i <= n; ++i)
- ++t[viz[i]].nrV;
- Sortare();
- }
- int main()
- {
- creare_MatAd();
- conex();
- ofstream fout ("componenteconexe3.out");
- fout << t[1].nod << " " << t[1].nrV;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement