#include <iostream>
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#include <stack>
#include <sstream>
#include <string>
using namespace std;
vector<vector<int> > a(26,vector<int>());
vector<int> dist,tim,ind;
stack<int> st;
bitset<26> vis;
void topo(int u){
vis[u]=1;
for(int i=0;i<a[u].size();i++)
if(!vis[a[u][i]])
topo(a[u][i]);
st.push(u);
}
int main(){
int t;
cin>>t;
while(t--){
int ed=0;
a.assign(26,vector<int>());
tim.assign(26,-1);
dist.assign(26,0);
ind.assign(26,0);
st=stack<int>();
vis.reset();
string s;
ws(cin);
while(getline(cin,s)&&s.size()>0){
stringstream ss(s);
char n,dep;
int day;
ss>>n>>day;
tim[n-'A']=day;
while(ss >> dep){
a[dep-'A'].push_back(n-'A');
ind[n-'A']++;
}
}
for(int i=0;i<26;i++)
if(!vis[i]&&tim[i]!=-1)
topo(i);
// queue<int> q;
// for(int i=0;i<26;i++)
// if(tim[i]!=-1&&ind[i]==0)
// q.push(i),dist[i]=tim[i];
while(!st.empty()){
int u=st.top();
if(dist[u]==0)
dist[u]=tim[u];
st.pop();
for(int i=0;i<a[u].size();i++){
if(dist[a[u][i]]<dist[u]+tim[a[u][i]])
dist[a[u][i]]=dist[u]+tim[a[u][i]];
}
}
int ans=0;
for(int i=0;i<26;i++)
ans=max(ans,dist[i]);
cout<<ans<<endl;
if(!cin.eof())
cout<<endl;
}
}
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#include <stack>
#include <sstream>
#include <string>
using namespace std;
vector<vector<int> > a(26,vector<int>());
vector<int> dist,tim,ind;
stack<int> st;
bitset<26> vis;
void topo(int u){
vis[u]=1;
for(int i=0;i<a[u].size();i++)
if(!vis[a[u][i]])
topo(a[u][i]);
st.push(u);
}
int main(){
int t;
cin>>t;
while(t--){
int ed=0;
a.assign(26,vector<int>());
tim.assign(26,-1);
dist.assign(26,0);
ind.assign(26,0);
st=stack<int>();
vis.reset();
string s;
ws(cin);
while(getline(cin,s)&&s.size()>0){
stringstream ss(s);
char n,dep;
int day;
ss>>n>>day;
tim[n-'A']=day;
while(ss >> dep){
a[dep-'A'].push_back(n-'A');
ind[n-'A']++;
}
}
for(int i=0;i<26;i++)
if(!vis[i]&&tim[i]!=-1)
topo(i);
// queue<int> q;
// for(int i=0;i<26;i++)
// if(tim[i]!=-1&&ind[i]==0)
// q.push(i),dist[i]=tim[i];
while(!st.empty()){
int u=st.top();
if(dist[u]==0)
dist[u]=tim[u];
st.pop();
for(int i=0;i<a[u].size();i++){
if(dist[a[u][i]]<dist[u]+tim[a[u][i]])
dist[a[u][i]]=dist[u]+tim[a[u][i]];
}
}
int ans=0;
for(int i=0;i<26;i++)
ans=max(ans,dist[i]);
cout<<ans<<endl;
if(!cin.eof())
cout<<endl;
}
}
Comments
Post a Comment