#include<bits/stdc++.h>usingnamespace std;typedef pair<int,int> pii;
vector<pii> st;int mp[10010]={0};int cnn[10010]={0};intsolve(int x){int ans =0;while(x){
ans +=(x %10)*( x %10);
x /=10;}return ans;}intis(int x){for(int i =2; i <=sqrt(x); i++)if(x % i ==0)return1;return2;}intmain(){int n , m;
cin >> n >> m;for(int i = m ; i >= n ; i --){if(mp[i])continue;int l = i ;int cnt =0;while( l !=1&& mp[l]!= i){
mp[l]= i;
l =solve(l);
cnt ++;}if(l ==1){
cnn[i]=is(i)* cnt;}else{
mp[i]=-1;}}for(int i = n ; i <= m ; i++){if(mp[i]== i) st.push_back({i , cnn[i]});}if(!st.size()){
cout<<"SAD"<<endl;return0;}sort(st.begin(), st.end());for(auto x : st){
cout<<x.first <<" "<<x.second<<endl;}return0;}intmain(){int _ =1;// scanf("%d",&_);while(_ --){solve();if(_)puts("");}return0;}
L2-031 深入虎穴 (25 分)
一定要等所有入度为0的点跑完来找最远,否则找到的不一定是最优解;或者在不断寻找路径长度时寻找最优解
#include<bits/stdc++.h>usingnamespace std;typedef pair<int,int> pii;
vector<vector<int>>st(100001);
set<int> mp;
vector<int>cn(100001);int ans =0, t =-1;voiddfs(int x ,int cnt){//mp[x] = true;for(int tt : st[x]){if(cnt +1> cn[tt])dfs(tt, cnt +1);//else ans = max(ans , cn[t]);}//mp[x] = false;
cn[x]= cnt;}intmain(){int n;
cin >> n;for(int i =1; i <= n ; i ++){int k ;
cin >> k;for(int j =0; j < k ; j ++){int s;
cin >> s;
st[i].push_back(s);
mp.insert(s);}//sort(st[i].begin() , st[i].end());}//dfs(1 , 1);for(int i =1; i <= n ; i ++){if(mp.count(i)==0)dfs(i ,1);}for(int i =1; i <= n ; i ++){if(ans < cn[i]){
ans = cn[i];
t = i;}}
cout<<t;return0;}