题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划

题解:刚开始理解题意理解了好半天,然后发现好像就是tarjan之后求个最长链?

然后题目规定了从1出发,那么就只将scc[1]放入初始队列。

然后只有10分。。。

后来我发现了这样的情况:因为只有scc[1]放入了队列,所以其它入度为0的并没有被放入队列,这使得一些点的inp无法减到0,以致无法更新答案

然后我就先从scc[1]dfs了一遍,先统计出每个点可通过scc[1]到达的度数,再拓扑排序,就过了。

好题,怒赞!

代码:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 550000
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,m,cnt,ti,tot,head[maxn],inp[maxn],dfn[maxn],low[maxn],sta[maxn],top;
int scc[maxn],s[maxn],f[maxn],a[maxn],b[maxn],c[maxn];
ll g[maxn];
struct edge{int go,next,w;}e[maxn];
queue<int> q;
inline void insert(int x,int y,int z)
{
e[++tot].go=y;e[tot].next=head[x];e[tot].w=z;head[x]=tot;
}
inline void tarjan(int x)
{
dfn[x]=low[x]=++ti;sta[++top]=x;
for(int i=head[x],y;i;i=e[i].next)
if(!dfn[y=e[i].go])
{
tarjan(y);low[x]=min(low[x],low[y]);
}
else if(!scc[y])low[x]=min(low[x],dfn[y]);
if(low[x]==dfn[x])
{
cnt++;int now=;
while(now!=x)
{
now=sta[top--];scc[now]=cnt;s[cnt]++;
}
}
}
inline void dfs(int x)
{
for(int i=head[x];i;i=e[i].next)
{
int y=e[i].go;
inp[y]++;
if(inp[y]==)dfs(y);
}
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();m=read();
for1(i,m){a[i]=read();b[i]=read();c[i]=read();insert(a[i],b[i],c[i]);}
for1(i,n)if(!dfn[i])tarjan(i);
memset(head,,sizeof(head));tot=;
for1(i,m)if(scc[a[i]]!=scc[b[i]])insert(scc[a[i]],scc[b[i]],c[i]);
for1(i,cnt)f[i]=,g[i]=inf;
f[scc[]]=g[scc[]]=s[scc[]];g[scc[]]--;
dfs(scc[]);
q.push(scc[]);
//for1(i,cnt)if(!inp[i])q.push(i),f[i]=s[i],g[i]=s[i]-1;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];i;i=e[i].next)
{
int y=e[i].go;
inp[y]--;
if(!inp[y])q.push(y);
if(f[x]+s[y]>f[y])f[y]=f[x]+s[y],g[y]=g[x]+e[i].w+s[y]-;
else if(f[x]+s[y]==f[y])g[y]=min(g[y],g[x]+e[i].w+s[y]-);
}
}
int ans=;
for1(i,cnt)
if(f[i]>f[ans])ans=i;
else if(f[i]==f[ans]&&g[i]<g[ans])ans=i;
printf("%d %lld\n",f[ans],g[ans]);
return ;
}

队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1的更多相关文章

  1. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  2. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

  3. CH Round #59 - OrzCC杯NOIP模拟赛day1

    第一题:队爷的新书 题意简述:给定n个闭区间,求出一个数p使它与包含它的区间数的积最大,输出这个积. 分析:使用一个差分数组g,每个区间[l,r],l位置加1,r+1的位置减1,从前往后统计,得到对于 ...

  4. 【强联通分量缩点】【最长路】【spfa】CH Round #59 - OrzCC杯NOIP模拟赛day1 队爷的讲学计划

    10分算法:对于城市网络为一条单向链的数据, 20分算法:对于n<=20的数据,暴力搜出所有的可能路径. 结合以上可以得到30分. 60分算法:分析题意可得使者会带着去的城市也就是这个城市所在强 ...

  5. 【离散化】【扫描线】CH Round #59 - OrzCC杯NOIP模拟赛day1 队爷的新书

    //上图绿色扫描线右侧少画了一条扫描线. 很多区间把数轴分成了很多段,看哪个点的(区间覆盖数*该点权值)最大. 显然在某个区间的右端点的答案是最优的. 排序后 用扫描线从左到右扫描,维护每个点的覆盖数 ...

  6. CH Round #58 - OrzCC杯noip模拟赛day2

    A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...

  7. CH Round #48 - Streaming #3 (NOIP模拟赛Day1)

    A.数三角形 题目:http://www.contesthunter.org/contest/CH%20Round%20%2348%20-%20Streaming%20%233%20(NOIP模拟赛D ...

  8. CH Round #54 - Streaming #5 (NOIP模拟赛Day1)

    A.珠 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2354%20-%20Streaming%20%235%20(NOIP模拟赛Day1)/珠 题解:sb题, ...

  9. CH Round #54 - Streaming #5 (NOIP模拟赛Day1)解题报告

    最近参加了很多CH上的比赛呢~Rating--了..题目各种跪烂.各种膜拜大神OTZZZ T1珠 描述 萌蛋有n颗珠子,每一颗珠子都写有一个数字.萌蛋把它们用线串成了环.我们称一个数字串是有趣的,当且 ...

随机推荐

  1. 用 ISNULL(), NVL(), IFNULL() and COALESCE() 函数替换空值

    在数据库操作中,往往要对一些查询出来的空值进行替换,如函数SUM(),这个函数如果没有值会返回NULL,这是我们不希望看到的, 在MySQL中我们可以这样来写: ) ... 在SQLSERVER中我们 ...

  2. spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务

    spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务 >>>>>>>>>>>> ...

  3. Js完美验证15/18身份证,Js验证身份证,支持15/18位

    Js完美验证15/18身份证,Js验证身份证,支持15/18位 >>>>>>>>>>>>>>>>> ...

  4. python sklearn.linear_model.LinearRegression.score

    score(self, X, y, sample_weight=None) 作用:返回该次预测的系数R2     其中R2 =(1-u/v).u=((y_true - y_pred) ** 2).su ...

  5. 服務器提交協議衝突 (The server committed a protocol violation.)

    ---解決方法 (放在 app.config / web.config)--- <system.net> <settings> <httpWebRequest useUn ...

  6. (转)Apache2 httpd.conf 配置详解 (二)

    转之--http://jafy00.blog.51cto.com/2594646/508205 DocumentRoot "/usr/local/apache-2.2.6/htdocs&qu ...

  7. For each db / table

    use master go exec master..sp_MSforeachdb 'use [?]; IF (SELECT db_id(''?'')) > 4 and (SELECT DATA ...

  8. Ubuntu12.04中安装ns-allinone-2.34

    1.首先安装ns2所需的组件.库之类: $sudo apt-get update $sudo apt-get install build-essential $ tcl8.-dev tk8. tk8. ...

  9. linux rman shell

    # make direcory for backset file and scripts file,in my case /backup/db_bak cd   /backup/db_bak mkdi ...

  10. SQL求差集

    数据库环境:SQL SERVER 2008R2 Sql Server有提供求集合差集的函数——EXCEPT.先看看EXCEPT的用法, { <query_specification> | ...