Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// https://informatics.msk.ru/mod/statements/view3.php?chapterid=164#1
- #include <bits/stdc++.h>
- using namespace std;
- int main(){
- int n, s;
- cin >> n >> s;
- int graph[n+1][n+1];
- vector < int > g[n+1];
- for (int i=1; i<=n; i++){
- for (int j=1; j<=n; j++){
- cin >> graph[i][j];
- if (graph[i][j] == 1){
- g[i].push_back(j);
- //g[j].push_back(i);
- }
- }
- }
- int used[n+1]; // used[i] = 1, якщо <i> Була відвідана
- for (int i=0; i<=n; i++) used[i] = 0;
- vector < int > q;
- q.push_back(s);
- used[s] = 1;
- int point = 0;
- while(point < q.size()){
- int v = q[point];
- for (int i=0; i<g[v].size(); i++){
- int to = g[v][i];
- if (used[to] == 0){
- used[to] = 1;
- q.push_back(to);
- }
- }
- point ++;
- }
- cout << q.size();
- return 0;
- }
Add Comment
Please, Sign In to add comment