This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <cmath>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <unordered_map>
using namespace std;
const int mx = 300001;
int dfs(int source , vector<vector<pair<int , int > > > &adj , vector<int> &r){
stack<int> st;
st.push(source);
vector<bool> visited(mx , false);
vector<int> stock;
stock.push_back(r[source]);
int cnt = 0;
while(!st.empty()){
int cur = st.top();
st.pop();
stock.push_back(r[cur]);
visited[cur] = true;
for(pair<int , int > i : adj[cur]){
if(count(stock.begin() , stock.end() , i.second) != 0 && visited[i.first] == false){
visited[i.first] = true;
st.push(i.first);
stock.push_back(r[i.first]);
cnt++;
}
}
}
return cnt;
}
vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> v, vector<int> c) {
vector<vector<pair<int , int > > > adj(mx);
vector<int> ff;
vector<int> ff1;
for(int i =0; i<u.size(); i++){
pair<int, int> rr;
rr.first = v[i];
rr.second = c[i];
if(count(ff.begin() , ff.end() , v[i]) == 0){
ff.push_back(v[i]);
adj[u[i]].push_back(rr);
pair<int , int > vv;
vv.first = u[i];
vv.second = c[i];
adj[v[i]].push_back(vv);
}
}
vector<int> you(r.size());
for(int i =0; i<r.size(); i++){
you[i] = dfs(i , adj , r);
}
vector<int> ans(r.size());
int y = *min_element(you.begin() , you.end());
for(int i = 0; i <you.size(); i++){
if(you[i] == y) ans[i] = 1;
else ans[i] = 0;
}
return ans;
}
/*
#1
4
0 1 1 2
5
0 0 1 1 3
1 2 2 3 1
0 0 1 0 2
#2
3
0 0 0
1
0
1
0
#3
7
0 1 1 2 2 1 2
10
0 0 1 1 2 3 3 4 4 5
1 2 2 3 3 4 5 5 6 6
0 0 1 0 0 1 2 0 2 1
*/
// int main(){
// int n; cin >> n;
// vector<int> r(n);
// for(int &i : r) cin >> i;
// int m; cin >> m;
// vector<int> u(m);
// vector<int> v(m);
// for(int &i : u) cin >> i;
// for(int &i : v) cin >> i;
// vector<int> c(m);
// for(int &i : c) cin >> i;
// // for(int i=0; i <= 10; i++){
// // cout << i << endl;
// // for(pair<int, int> j : adj[i]){
// // cout << i << " " << j.first << " " << j.second << endl;
// // }
// // cout << endl;
// // }
// for(int i =0; i <n; i++){
// vector<int> ans = find_reachable(r , u , v, c);
// for(int i :ans){
// cout << i << " ";
// }
// cout << endl;
// }
// return 0;
// }
Compilation message (stderr)
keys.cpp: In function 'std::vector<int> find_reachable(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
keys.cpp:71:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i =0; i<u.size(); i++){
| ~^~~~~~~~~
keys.cpp:88:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(int i =0; i<r.size(); i++){
| ~^~~~~~~~~
keys.cpp:93:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int i = 0; i <you.size(); i++){
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |