Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- void main() {
- setlocale(LC_ALL, "Russian");
- // считываем количество вершин, ребер и номер вершины, для которой будем решать задачу
- int n, m;
- cin >> n >> m;
- // создаем вектор, в котором будем хранить граф
- vector<vector<bool>> g(n, vector<bool>(n));
- // считываем ребра и записываем в вектор
- for (int i = 0; i < m; i++) {
- int from, to;
- cin >> from >> to;
- g[from][to] = true;
- g[to][from] = true;
- }
- // перебираем строки в матрице смежности
- for (int i = 0; i < n; i++) {
- // для каждой строки посчитаем сколько в ней стоит true
- // это и будет количеством ребер этой вершины
- int cnt = 0;
- for (int j = 0; j < n; j++) {
- if (g[i][j]) cnt++;
- }
- cout << "v[" << i << "]=" << cnt << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement