L2-031 深入虎穴 - dfs找最大深度
思路:
这题和小字辈很像
【PTA-训练day14】L2-026 小字辈 + L1-054 福到了_Roye_ack的博客-CSDN博客
- 先找到入口,入口就是所有门都不能通往的那个 可以用一个st数组把出现的门都置为true 则遍历所有门 如果st[i]为false的则为入口
- 通过入口dfs 搜寻最大深度 并及时记录最大深度所对应的门编号
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,maxx=-1,res;
vector<vector<int>> v;
void dfs(int u,int d)
{
if(v[u].size()==0)
if(maxx<d)
{
maxx=d;
res=u;
}
for(int i=0;i<v[u].size();i++)
dfs(v[u][i],d+1);
}
int main()
{
cin>>n;
bool st[n+1]={false};
v.resize(n+1); //因为vector是动态的 所以行要固定
for(int i=1;i<=n;i++)
{
int k;
cin>>k;
while(k--)
{
int x;
cin>>x;
v[i].push_back(x);
st[x]=true;//入口是其他门都不能通往的那个 则可以通往的门标为true 入口就是false
}
}
for(int i=1;i<=n;i++) if(!st[i]) dfs(i,1);
cout<<res;
}
L1-079 天梯赛的善良 - 20 - 弱智题
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,maxx=-1,minx=0x3f3f3f3f,c1=0,c2=0;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
maxx=max(maxx,a[i]);
minx=min(minx,a[i]);
}
for(int i=0;i<n;i++)
{
if(a[i]==maxx) c1++;
if(a[i]==minx) c2++;
}
cout<<minx<<" "<<c2<<endl<<maxx<<" "<<c1;
}
L1-077 大笨钟的心情 - 15
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[25];
for(int i=0;i<24;i++) cin>>a[i];
while(1)
{
int x;
cin>>x;
if(x<0||x>23) break;
cout<<a[x]<<" ";
if(a[x]>50) puts("Yes");
else puts("No");
}
}
L1-075 强迫症 - 10
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int n=s.size();
if(n==6) cout<<s.substr(0,4)<<"-"<<s.substr(4);
else{
string str=s.substr(0,2);
int t=stoi(str);
if(t<22) cout<<"20";
else cout<<"19";
cout<<str<<"-"<<s.substr(2);
}
}