CUC-SUMMER-9-C

C - Longtail Hedgehog
Codeforces Round #338 (Div. 2)

This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions:
Only segments already presented on the picture can be painted;
The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment;
The numbers of points from the beginning of the tail to the end should strictly increase.

Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get.
Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.

Input
First line of the input contains two integers n and m(2?≤?n?≤?100?000, 1?≤?m?≤?200?000) — the number of points and the number segments on the picture respectively.
Then follow m lines, each containing two integers ui and vi (1?≤?ui,?vi?≤?n, ui?≠?vi) — the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.

Output
Print the maximum possible value of the hedgehog's beauty.

Example
Input
8 64 53 52 51 22 86 7

Output
9

Input
4 61 21 31 42 32 43 4

Output
12

Note
The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3·3?=?9.



題意:有一個(gè)圖蒋失,你把它看成一個(gè)刺猬矾兜,刺猬有一個(gè)尾巴,尾巴滿足的條件是從尾巴起始結(jié)點(diǎn)到尾巴終止結(jié)點(diǎn)是遞增的鸯隅,刺猬還有刺邦尊,刺滿足的條件是刺有一個(gè)長(zhǎng)度并與尾巴終止結(jié)點(diǎn)相連,求尾巴長(zhǎng)度與刺個(gè)數(shù)乘積的最大值

解法:刺猬的每個(gè)結(jié)點(diǎn)都可以看作尾巴的終點(diǎn),且此結(jié)點(diǎn)的度即為尾巴的長(zhǎng)度,所以要求每個(gè)結(jié)點(diǎn)作為尾巴終點(diǎn)時(shí)尾巴的長(zhǎng)套腹,dfs即可

代碼:

#include<iostream>
#include<vector>
using namespace std;
int n,m;
vector<int> v[100005];
long long degree[100005];
long long dp[100005];
bool vis[100005];
void dfs(int a)
{
    vis[a]=1;
    for(int i=0;i<v[a].size();i++){
        if(v[a][i]<a){
            if(!vis[v[a][i]])
                dfs(v[a][i]);
            dp[a]=max(dp[a],dp[v[a][i]]+1);
        }
    }
}
int main()
{
    int x,y;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        dp[i]=1;
    for(int i=0;i<m;i++){
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
        degree[x]++;
        degree[y]++;
    }
    for(int i=1;i<=n;i++)
        if(!vis[i])
            dfs(i);
    long long ans=0;
    for(int i=1;i<=n;i++)
        if(dp[i]*degree[i]>ans)
            ans=dp[i]*degree[i];
    cout<<ans<<endl;
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市资铡,隨后出現(xiàn)的幾起案子电禀,更是在濱河造成了極大的恐慌,老刑警劉巖害驹,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異蛤育,居然都是意外死亡宛官,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)瓦糕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)底洗,“玉大人,你說(shuō)我怎么就攤上這事咕娄『ヒ荆” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵圣勒,是天一觀的道長(zhǎng)费变。 經(jīng)常有香客問(wèn)我,道長(zhǎng)圣贸,這世上最難降的妖魔是什么挚歧? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮吁峻,結(jié)果婚禮上滑负,老公的妹妹穿的比我還像新娘。我一直安慰自己用含,他們只是感情好矮慕,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著啄骇,像睡著了一般痴鳄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上缸夹,一...
    開(kāi)封第一講書(shū)人閱讀 51,274評(píng)論 1 300
  • 那天夏跷,我揣著相機(jī)與錄音哼转,去河邊找鬼。 笑死槽华,一個(gè)胖子當(dāng)著我的面吹牛壹蔓,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播猫态,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼佣蓉,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了亲雪?” 一聲冷哼從身側(cè)響起勇凭,我...
    開(kāi)封第一講書(shū)人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎义辕,沒(méi)想到半個(gè)月后虾标,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡灌砖,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年璧函,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片基显。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蘸吓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出撩幽,到底是詐尸還是另有隱情库继,我是刑警寧澤,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響谱净,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜雨膨,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望读串。 院中可真熱鬧聊记,春花似錦、人聲如沸恢暖。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)杰捂。三九已至舆床,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背挨队。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工谷暮, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人盛垦。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓湿弦,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親腾夯。 傳聞我的和親對(duì)象是個(gè)殘疾皇子颊埃,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容