2025/3/15

#include<iostream>
#include<vector>
using namespace std;
int n;
vector <int>a[1555];
bool head[1555];
int self[1555],kids[1555];
void dp(int fa,int u)
{
for(int i=0;i<a[u].size();i++)
{
int v=a[u][i];
if(v==fa)continue;
dp(u,v);
kids[u]+=self[v];
self[u]+=min(self[v],kids[v]);
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
int x,m;
cin>>x>>m;
for(int j=1;j<=m;j++)
{
int y;
cin>>y;
a[x].push_back(y); 
a[y].push_back(x);
}
}
for(int i=0;i<=n-1;i++)self[i]=1;
dp(-1,0);
cout<<min(self[0],kids[0]);
// for(int j=0;j<=n-1;j++)cout<<self[j]<<" "<<kids[j]<<"\n"; 
// for(int i=0;i<=n-1;i++)
// {
// for(int j=0;j<a[i].size();j++)
// cout<<a[i][j]<<" ";
// cout<<"\n";
// }
return 0;
}
 
 
 
 
#include<iostream>
using namespace std;
int n,head[10010],tot=0;
struct EDGE
{
int to,next,weight;
}edge[10010];
void add(int a,int b,int c)
{
edge[++tot].weight=c;
edge[tot].to=b;
edge[tot].next=head[a];
head[a]=tot;
}
int longest[10010],longer[10010],falong[10010],inlong[10010];
int dfs(int u,int fa) 
{
if(longest[u]>0) return longest[u];
for(int i=head[u];i;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa)continue;
if(longest[u]<dfs(v,u)+edge[i].weight)
{
inlong[u]=v;
longer[u]=longest[u];
longest[u]=dfs(v,u)+edge[i].weight;
}
else if(longer[u]<dfs(v,u)+edge[i].weight)
longer[u]=dfs(v,u)+edge[i].weight;
}
return longest[u];
}
void dfs2(int u,int fa)
{
for(int i=head[u];i;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa)continue;
if(inlong[u]==v)falong[v]=max(falong[u],longer[u])+edge[i].weight;
else falong[v]=max(falong[u],longest[u])+edge[i].weight;
dfs2(v,u);
// cout<<u<<" "<<fa<<" "<<v<<" "<<falong[v]<<"\n";
}
}
int main()
{
cin>>n;
for(int i=2;i<=n;i++)
{
int y,w;
cin>>y>>w;
add(i,y,w);
add(y,i,w);
}
dfs(1,-1);
dfs2(1,-1);
for(int i=1;i<=n;i++)cout<<max(longest[i],falong[i])<<"\n";
// for(int i=1;i<=n;i++)cout<<longest[i]<<" "<<longer[i]<<" "<<falong[i]<<" "<<inlong[i]<<"\n";
return 0;
}
 
 
 
 
 
 
 
#include<iostream>
#include<cstring>
using namespace std;
string c;
int main()
{
    cin>>c;
    for(int i=0;i<c.length();i++)if(int(c[i])>90)c[i]=char(c[i]-32);
    char ch=c[0];
    int cnt=0;
    for(int i=0;i<c.length();i++)
        if(ch==c[i])cnt++;
        else
        {
            cout<<"("<<c[i-1]<<","<<cnt<<")";
            cnt=1;
            ch=c[i];
        }
    cout<<"("<<c[c.length()-1]<<","<<cnt<<")";
    return 0;
}
posted @ 2025-04-29 21:10  永韶  阅读(8)  评论(0)    收藏  举报